Smarty3手册 联系客服

发布时间 : 星期五 文章Smarty3手册更新完毕开始阅读80abdced5ef7ba0d4a733b3f

{$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()} 新增加了一个{function}的标签,可以定义一个可供调用的函数块(我喜欢这功能,哈哈!)

{function}...{/function} 该标签必须有一个name属性,用来指名该函数名称,也是调用的时候需要用到的

下面是一个例子

{function name=menu level=0}

    {$entry@key} {menu data=$entry level=$level+1} {else}
  • {$entry}
  • {/if} {/foreach}
{/function} {$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' => ['item3-3-1','item3-3-2']],'item4']} {menu data=$menu} {function}功能函数必须有一个name属性,并且可以拥有任意多个的其它属性 代码块不缓存,可以使用{nocache}标签默认是关闭的

{nocache} ... {/nocache} 还可以作为属性 {$foo nocache=true} {$foo nocache} {foo bar=\bar=\ 返回当前模板的方法 $smarty.cur_template 变量作用域和存储

在Smarty2中,所有的变量都存储在Smarty对象中,因此所有的变量在所有模板和子方法中都可以获取

在Smarty3中,可以自己定义的将变量存储在主Smarty对象中,或者用户自己定义的对象中,甚至是用户自己的模板对象中 而且这些对象可以通过链式串接起来。

在链的末尾的对象可以获取到对象链之前的对象中存储的所有变量。

Smarty对象必须是链的根对象,但是对象链却是可以独立于Smarty对象存在的 所有的Smarty的赋值方法都可以用在data对象或者模板对象 除了上面说几个方面,全局变量还有一种特殊的存储方式 一个Smarty的数据对象(data Object)可以通过如下方式创建

$data = $smarty->createData(); // 创建根数据对象 $data->assign('foo','bar'); // 赋值操作 $data->config_load('my.conf'); // 加载配置文件 $data = $smarty->createData($smarty); // 以Smarty作为父对象,创建数据对象 $data2= $smarty->createData($data); // 以data作为父对象,创建数据对象data2 创建一个模板对象(template object) 可以通过createTemplate方法,它的参数传递和fetch()/display()方法一致 函数定义方式

function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null) 举例 $tpl = $smarty->createTemplate('mytpl.tpl'); // 创建一个模板对象,没有父对象 $tpl->assign('foo','bar'); // directly assign variables $tpl->config_load('my.conf'); $tpl = $smarty->createTemplate('mytpl.tpl',$smarty); // 以Smarty为父对象,创建模板对象 fetch()/display() 两个方法将隐式的创建一个模板对象 如果不指定父对象,则默认父对象将指向Smarty对象

如果一个模板是通过include方式调用的,则子模板的父对象将指向引用它的模板对象

所有当前模板变量和父对象的模板变量都是可以获取的,但是如果是通过{assign}或者{$foo=…}这样的方法创建或者修改变量 则它的作用域将只停留在当前模板对象

Smarty3中,在赋值变量的时候可以指定它的作用域,有4个值local,parent,root,global

{assign var=foo value='bar'} // no scope is specified, the default 'local' {$foo='bar'} // same, local scope {assign var=foo value='bar' scope='local'} // same, local scope {assign var=foo value='bar' scope='parent'} // Values will be available to the parent object {$foo='bar' scope='parent'} // (normally the calling template) {assign var=foo value='bar' scope='root'} // Values will be exported up to the root object, so they can {$foo='bar' scope='root'} // be seen from all templates using the same root. {assign var=foo value='bar' scope='global'} // Values will be exported to global variable storage, {$foo='bar' scope='global'} 扩展

Smarty3的扩展都是继承至Smarty – Internal – PluginBase的类 所有的扩展都包含一个Smarty对象实例的$this->smarty属性

模板继承

你可以在模板中写{block} … {/block}快,并且这些块可以在子模板中进行覆盖

parent.tpl:

{block name='title'}My site name{/block}

{block name='page-title'}Default page title{/block}

child.tpl: