表达式
支持更加随意的表达式
{$x+$y} 输入x和y的和 {$foo = strlen($bar)} 变量支持PHP函数 {assign var=foo value= $x+$y} 属性支持表达式 {$foo = myfunct( ($x+$y)*3 )} 函数参数支持表达式 {$foo[$x+3]} 数组下表支持表达式可以在Smarty标签使用其他标签的值{$foo="this is message {counter}"}可以在双引号里使用Smarty标签
{$foo="this is message {counter}"}双引号中可以使用变量,也可以使用Smarty标签
{$foo="this is message $counter"} {func var="test $foo test"} // 同$foo {func var="test $foo_bar test"} // 同$foo_bar {func var="test `$foo[0]` test"} // 同$foo[0] {func var="test `$foo[bar]` test"} // 同$foo[bar] {func var="test $foo.bar test"} // 同$foo (而不是$foo.bar) {func var="test `$foo.bar` test"} // 同$foo.bar {func var="test `$foo.bar` test"|escape} // 引号外加修饰符 {func var="test {$foo|escape} test"} // 引号内加修饰符 {func var="test {time()} test"} // 调用PHP方法 {func var="test {counter} test"} // 调用插件返回值 {func var="variable foo is {if !$foo}not {/if} defined"} // 调用Smarty block方法 {* 将会替换$tpl_name为对应的值*} {include file="subdir/$tpl_name.tpl"} {* 不会替换$tpl_name *} {include file='subdir/$tpl_name.tpl'} // 变量需要用双引号才可以解析! {* 用点号"."的话必须加反引号 *} {cycle values="one,two,`$smarty.config.myval`"} {* 用点号"."的话必须加反引号 *} {include file="`$module.contact`.tpl"} {* 用点号语法可以使用变量 *} {include file="`$module.$view`.tpl"}可以在模板里头定义数组
{assign var=foo value=[1,2,3]} {assign var=foo value=['y'=>'yellow','b'=>'blue']} {assign var=foo value=[1,[9,8],3]} 数组可以嵌套简单的变量赋值
{$foo=$bar+2}可以给指定的数组元素赋值,如果变量存在但不是数组,会先转换成数组,再进行赋值{$foo['bar']=1} {$foo['bar']['blar']=1}同上,可以给数组添加值
{$foo[]=1}点号.功能更强大:支持变量索引、支持表达式索引、支持嵌套索引
{$foo.a.b.c} => $foo['a']['b']['c']
{$foo.a.$b.c} => $foo['a'][$b]['c'] 支持变量索引
{$foo.a.{$b+4}.c} => $foo['a'][$b+4]['c'] 支持表达式索引
{$foo.a.{$b.c}} => $foo['a'][$b['c']] 支持嵌套索引变量名中支持变量
$foo 一个普通的变量 $foo_{$bar} 变量名中包含变量 $foo_{$x+$y} 变量名中可以支持表达式 $foo_{$bar}_buh_{$blar} 变量名包含多个变量 {$foo_{$x}} 如果$x是1,则输出$foo_1支持对象链,即是对象方法的连续调用,很像jquery
{$object->method1($x)->method2($y)}{for}标签支持类似loop一样的循环
{for $x=0, $y=count($foo); $x<$y; $x++} .... {/for}在FOR循环中可以通过如下特殊标示符限定位置:
$x@iteration 当前循环次数 $x@total 总循环次数 $x@first 循环第一次 $x@last 循环最后一次新的foreach语法
{foreach $myarray as $var}...{/foreach}同样是foreach里头的特殊表示符,看的就明白,不翻译了……
$var@key foreach $var array key $var@iteration foreach current iteration count (1,2,3...) $var@index foreach current index count (0,1,2...) $var@total foreach $var array total $var@first true on first iteration $var@last true on last iteration支持while循环
{while $foo}...{/while} {while $x lt 10}...{/while}可以直接使用PHP的函数
{time()} 支持将字符串作为模板显示:使用string的资源类型即可:$smarty->display('string:This is my template, {$foo}!'); // php {include file="string:This is my template, {$foo}!"} // template支持纯php的模板:完全以php语法书写的模板,调用时使用php的资源类型即可:$smarty->display(‘php:foo.php’); 当然也可以{include file=”php:foo.php”}, 不过在纯php的模板中是无法调用smarty的modifier/function的,以后的版本也许会考虑封装模板继承:类似类的继承,可以继承父模板文件,同时重载父模板中相同name的{block}区块内容
静态类,命名空间的支持:可以这样注册一个静态类,其中命名空间可选
$smarty->register->templateClass(‘foo’,'name\name2\myclass’);
在模板中调用:{foo::method()}
参考:http://www.emptykid.com/blog/archives/133
http://shameerc.wordpress.com/2010/10/06/an-introduction-to-smarty-3/




