smarty使用指导1-smarty配置实例

1. 下载smarty工具包;
2. 解压smarty,在这个目录下创建新目录cache,configs,template,templates_c;
3. 在根目录下创建smarty_inc.php文件,内容如下:

<?php
include_once("./libs/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 = "}>"; //右定界符
?>
4. 创建test.php文件,内容如下:
<?php
include("smarty_inc.php");
$val= array("ShuaiYan","zcping","qiqi","Yangyang");
$smarty->assign("name",$val);
$smarty->display("index2.tpl");
?>

5. 在templates目录下创建index2.tpl,内容如下(这个地方要小心,需要注意文件的存储编码必须是utf-8,其他形式的编码要么是文件乱码,要么是无法执行):
<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>

 

1