@mircode
2016-07-12T03:36:00.000000Z
字数 5566
阅读 835
smarty
Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑分离。简单的讲,目的就是要使PHP程序员同前端人员分离,使程序员改变程序的逻辑内容不会影响到前端人员的页面设计,前端人员重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。
模板注释被*星号包围,而两边的星号又被定界符包围。
例如:
{* this is a comment *}
注意:
Smarty注释不会在模板文件的最后输出中出现,这与HTML注释不同。
模板变量用美元符号$开始,可以包含数字、字母和下划线。
{assign var=foo value="helloword"}{*{assign}用来在模板运行时为模板变量赋值*}{$foo}{*${foo}来引用变量*}
*注意
GET、SESSION等PHP全局变量,可以通过$smarty引用。
{* display value of page from URL ($_GET) http://www.example.com/index.php?page=fo{$smarty.get.page}{* display the variable "page" from a form ($_POST['page']) *}{$smarty.post.page}
每一个smarty标签输出一个变量或者调用某种函数。在定界符内函数(一般定界符‘{}’包住)和其属性(同样在定界符内)将被处理和输出。例如: {funcname attr1="val" attr2="val"}
注意:
函数的属性实际为函数的参数。
Smarty自带了一些内置函数,这些内置函数是Smarty模板引擎的组成部分。他们被编译成相应的内嵌PHP代码,以获得最大性能。
如:
{$var=}{append}{assign}{block}{call}
这里,我们找一些典型的函数介绍一下。
改标签用于在当前模板中包含其它模板。当前模板中的任何有效变量在被包含模板中同样可用。
<html><head><title>{$title}</title></head><body>{*引入页面头*}{include file='page_header.tpl'}{*引入页面主题内容,支持file属性为$变量引用*}{include file="$page_content.tpl"}{*引入页面尾巴*}{include 'page_footer.tpl'}</body></html>
2、条件
{if isset($name) && $name == 'Blog'}{* do something *}{elseif $name == $foo}{* do something *}{/if}{if is_array($foo) && count($foo) > 0}{* do a foreach loop *}{/if}
3、循环函数
<?php$arr = array('red', 'green', 'blue');$smarty->assign('myColors', $arr);?>Template to output $myColors in an un-ordered list<ul>{foreach $myColors as $color}<li>{$color}</li>{/foreach}</ul>The above example will output:<ul><li>red</li><li>green</li><li>blue</li></ul>
用户可以使用Smarty自带的一组自定义函数。当然我们也可以自己自定义函数。(详见第四章节)
{html_checkboxes}{html_image}{html_options}{html_radios}{html_select_date}{html_select_time}{html_table}
2.0版本引入了被广泛应用于自定义Smarty函数功能的插件架构。它包括如下类型:
functions 函数插件
modifiers 调节器插件
block functions 块函数插件
compiler functions 编译函数插件
prefilters 预滤器插件
postfilters 后滤器插件
outputfilters 输出过滤器插件
resources 资源插件
inserts 嵌入插件
插件文件必须命名如下:
type.name.php
模板传递给模板函数的所有属性都包含在关联数组params中。
在模板中,函数的输出内容(返回值)在原位置用函数标签代替,例如{fetch}函数。作为另一种选择,函数也可以单纯地用来做些非输出内容的任务,如{assign}函数。
如果函数需要分配(俗话说的赋值)一些变量给模板或者使用Smarty提供的一些函数,可以通过smarty对象实现,如$smarty->foo()。
例如:带输出的函数插件
<?php/** Smarty plugin* -------------------------------------------------------------* File: function.eightball.php* Type: function* Name: eightball* Purpose: outputs a random magic answer* -------------------------------------------------------------*/function smarty_function_eightball($params, $smarty){$answers = array('Yes','No','No way','Outlook not so good','Ask again soon','Maybe in your reality');$result = array_rand($answers);return $answers[$result];}?>which can be used in the template as:Question: Will we ever have time travel?Answer: {eightball}.
块函数的形式是这样的:{func} .. {/func}。换句话说,他们被封闭在一个模板区域内,然后对该区域的内容进行操作。块函数优先于同名的自定义函数,即是说,你不能同时使用自定义函数{func}和块函数{func} .. {/func}。
默认地,你的函数实现会被Smarty调用两次:一次是在开始标签,另一次是在闭合标签(参考下面的repeat关于怎样改变这种设置)。
param只有块函数的开始标签具有属性。所有属性包含在作为关联数组的params变量中,经由模板传递给模板函数。当处理闭合标签时,函数同样可访问开始标签的属性。
content变量值取决于你的函数是被开始标签调用还是被闭合标签调用。假如是开始标签,变量值将为NULL,如果是闭合标签,content变量值为模板块的内容。请注意这时模板块已经被Smarty处理过,因此你所接收到的是模板的输出而不是模板资源。
repeat参数通过引用传递给函数执行,并为其提供控制块显示多少次的可能性。默认情况下,在首次调用块函数(块开始标签)时repeat变量为true,在随后的所有块函数(闭合标签)调用中其值始终为false。函数每次执行返回的repeat值为true时,{func} .. {/func}之间的内容会被求值,同时参数$content里的新块内容会再次调用执行函数。
<?php/** Smarty plugin* -------------------------------------------------------------* File: block.translate.php* Type: block* Name: translate* Purpose: translate a block of text* -------------------------------------------------------------*/function smarty_block_translate($params, $content, $smarty, &$repeat){// only output on the closing tagif(!$repeat){if (isset($content)) {$lang = $params['lang'];// do some intelligent translation thing here with $contentreturn $translation;}}}?>
调节器插件的第一个参数应该直接了当地声明处理什么类型(可以是字符串、数组、对象等等这些类型)。其它的参数是可选的,取决于执行的操作类型。
调节器必须返回处理结果。
例如:日期调节器
{$smarty.now|date_format}{$smarty.now|date_format:"%A, %B %e, %Y"}{$smarty.now|date_format:"%H:%M:%S"}
自定义调节器
<?php/** Smarty plugin* -------------------------------------------------------------* File: modifier.capitalize.php* Type: modifier* Name: capitalize* Purpose: capitalize words in the string* -------------------------------------------------------------*/function smarty_modifier_capitalize($string){return ucwords($string);}?>
其他形式的函数,请自行查询Smarty的文档
Smarty模板手册
{*声明一个数组变量*}{assign var=colors value=['red', 'green', 'blue']}{colors[1]} {*访问第一个元素*}{*分支和循环*}{foreach colors as $color}{if $color=='red'}<li style='color:red'>{$color}</li>{else}<li>{$color}</li>{/if}{/foreach}{*函数插件:随机回答*}function smarty_function_eightball($params, $smarty){$answers = array('Yes','No');$result = array_rand($answers);return $answers[$result];}eg:Question: Will we ever have time travel?Answer: {eightball}.{*块函数插件:将translate标签中的内容进行翻译*}function smarty_block_translate($params, $content, $smarty, &$repeat){// only output on the closing tagif(!$repeat){if (isset($content)) {$lang = $params['lang'];// do some intelligent translation thing here with $contentreturn $translation;}}}eg: {translate lang='zh_CN'}hello{/translate} => 你好{*调节器插件:首字母大写*}function smarty_modifier_capitalize($string){return ucwords($string);}eg: {$str|capitalize}