Smarty 常用操作

路径设置

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中的{}使用时前后需要留出空格,否则会引起混淆导致失败
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值