smarty模板配置从零开始1
在Smarty的demo目录下存在templates,templates_c,configs,cache目录,把这些目录拷贝到
根目录,如果下载的模板不存在上面的
建立smarty_inc.php文件对smarty进行配置如下:
<?php
include_once("./smarty/Smarty.class.php"); //包含smarty类文件,这个目录按照设置修改
$smarty = new Smarty(); //建立smarty实例对象$smarty
$smarty->caching=false; //开发是不建议开启缓存
$smarty->template_dir="./templates"; //设置模板目录
$smarty->compile_dir="./templates_c"; //设置编译目录
$smarty->cache_dir="./cache"; //缓存文件夹
$smarty->cache_lifetime=60;
$smarty->left_delimiter = "<{"; //左定界符
$smarty->right_delimiter = "}>"; //右定界符
?>
在根目录下建立index.php文件:
<?php
include("smarty_inc.php");
$val= array("Shuaiyan","zcping","Qiqi","Yangyang");
$smarty->assign("name",$val);
$smarty->display("index2.tpl");
?>
在templates下建立index.html模板:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8">
<title>测试页面</title>
</head>
<body>
<{foreach from=$name item=x}>
数组内容:<{$x}> <br/>
<{/foreach}>
</body>
</html>
如果案例中出现乱码的情况,一般是因为文件的格式有问题,只需要用记事本打开,
然后另存文件到特定格式即可。
参考:http://www.phpddt.com/mvc/115.html