PHP头条
热点:

php模板引擎smarty的内置函数之二


Smarty自带一些 内建函数.
内建函数是模板语言的一部分.
用户不能创建名称和 内建函数一样的自定义函数,也不能修改 内建函数 .
(#capture函数、config_load 、foreach,foreachelse、include、include_php等内置函数请参考php模板引擎smarty使用教程专题之 内置函数之一)
insert
if,elseif,else
ldelim,rdelim
literal
php
section,sectionelse
strip
 
insert
属性 类型 是否必须 缺省值 描述
name string Yes n/a  插入函数的名称
assign string No n/a  该属性指定一个变量保存待插入函数输出
script string No n/a  插入函数前需要先包含的php脚本名称
 
[var ...] [var type] No n/a  传递给待插入函数的本地参数 Insert 函数类似欲 inluce 函数,不同之处是 insert 所包含的内容不会被缓存,每次调用该模板都会重新执行该函数. 例如你在页面上端使用一个带有广告条位置的模板,广告条可以包含任何HTML、图象、FLASH等混合信息. 因此这里不能使用一个静态的链接,同时我们也不希望该广告条被缓存. 这就需要在 insert 函数指定:#banner_location_id# 和 #site_id# 值(从配置文件中取),同时需要一个函数取广告条的内容信息.
 
insert 函数演示
{* example of fetching a banner *} {insert name="getBanner" lid=#banner_location_id# sid=#site_id#} 在此例中,我们使用了 getBanner 作为 name 属性,同时传递了 #banner_location_id# 和 #site_id# 两个参数. 接下来 Smarty 在你的 php 程序中搜索名为 insert_getBanner() 的函数,#banner_location_id# 和 #site_id# 的值被组合成一个数组作为函数的第一个参数传递给该函数. 为了避免函数命名混乱,所有的 insert 函数都必须以 insert_ 开头. 你的 insert_getBanner() 函数根据传递的参数执行并返回执行的结果. 这些结果就显示在模板中调用该函数的位置. 在此例中 Smarty 调用该函数类似insert_getBanner(array("lid"=>"12345","sid"=>67890"));并将返回的结果显示在调用的位置.
 
如果设置了 assign 属性,该属性对应的变量名用于保存待包含函数的输出,这样待包含函数的输出就不会直接显示了.注意:赋给模板变量的输出信息在缓存的时候同样无效.
 
如果指定了 script 属性,在调用函数并执行前将先包含(只包含一次)script指定的 php 脚本. 这是为了防止被调用的函数不存在,先调用包含该函数的 php 脚本将避免该情况.
 
Smart 对象作为函数的第二个参数被传递,在待包含函数中可以通过 $this 访问并修改 smarty 对象信息.
技术要点: 使模板的一部分不被缓存. 如果打开了缓存, insert 函数却不会被缓存,每次调用页面它们都会被动态加载,即使是在缓存页面中. 该特性可以广泛应用于广告条、投票、实时天气预报、搜索结果、反馈信息等区域.
 
if,elseif,else
 
Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用,并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句. 可以使用以下条件修饰词: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、==、!=、>、<、<=、>=. 使用这些修饰词时必须和变量或常量用空格格开.
语句演示 {if $name eq "Fred"} Welcome Sir.{elseif $name eq "Wilma"} Welcome Ma'am.{else} Welcome, whatever you are.{/if}{* an example with "or" logic *}{if $name eq "Fred" or $name eq "Wilma"} ...{/if}{* same as above *}{if $name == "Fred" || $name == "Wilma"} ...{/if}{* the following syntax will NOT work, conditional qualifiers must be separated from surrounding elements by spaces *}{if $name=="Fred" || $name=="Wilma"} ...{/if}{* parenthesis are allowed *}{if ( $amount < 0 or $amount > 1000 ) and $volume >= #minVolAmt#} ...{/if}{* you can also embed php function calls *}{if count($var) gt 0} ...{/if}{* test if values are even or odd *}{if $var is even} ...{/if}{if $var is odd} ...{/if}{if $var is not odd} ...{/if}{* test if var is divisible by 4 *}{if $var is div by 4} ...{/if}{* test if var is even, grouped by two. i.e.,0=even, 1=even, 2=odd, 3=odd, 4=even, 5=even, etc. *}{if $var is even by 2} ...{/if}{* 0=even, 1=even, 2=even, 3=odd, 4=odd, 5=odd, etc. *}{if $var is even by 3} ...{/if} ldelim,rdelim
ldelim 和 rdelim 用于输出分隔符,也就是大括号 "{" 和 "}". 模板引擎总是尝试解释大括号内的内容,因此如果需要输出大括号,请使用此方法.
使用 ldelim, rdelim 演示 {* this will print literal delimiters out of the template *}{ldelim}funcname{rdelim} is how functions look in Smarty!输出结果:{funcname} is how functions look in Smarty!
literal
Literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示.
 
literal 标签演示
{literal}
 

相关文章

相关频道:

PHP之友评论

今天推荐