路径设置
templates/
templates_c/
cache/
configs/
libs/
注意:templates_c和cache两个目录的权限要设置成777(chmod -R 777 templates_c cache)
首次使用
#在templates目录下增加一个demo.tpl的文件,里面写上hello world
index.php文件如下:
<?php
require('./libs/Smarty.class.php');
$smarty = new Smarty;
//$smarty->assign("result", $res);
$smarty->display('demo.tpl');
?>
循环条件语句
#判断语句:
{if $name eq "Fred"}
Welcome Sir.
{elseif $name eq "Wilma"}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
#循环语句:
{foreach from=$data.list item=one name=f}
{if $smarty.foreach.f.iteration<4}
#判断循环次数
{/if}
{/foreach}
#其它:
1)对字母作截断显示
{$NAME|truncate:16:"..."}
2)urlencode:
{$data|escape:"url"}
3)嵌套
{include file="block/sideshare.tpl"}
以templates为起始目录
可以使用以下条件修饰词:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、div by、even by、odd by、==、!=、>、<、<=、>=. 使用这些修饰词时必须和变量或常量用空格格开
原样输出
#模板中原样输出,使用literal标签
{literal}
{/literal}
修改分隔符
1.改源码:
Smarty.class.php $left_delimiter 不推荐<br>
2.动态修改:
$Smarty->left_delimiter="<;{";
$Smarty->right_delimiter="}>";
#写在display之前
注意事项
#templates_c和cache两个目录的权限要设置成777,否则无法打开网页
#js中的{}使用时前后需要留出空格,否则会引起混淆导致失败