大多数函数都带有自己的属性以便于明确说明或者修改他们的行为,smarty函数的属性很像HTML中的属性。静态数值不需要加引号,但是字符串建议使用引号。可以使用普通smarty变量,也可以使用带调节器的变量作为属性值,它们也不用加引号。你甚至可以使用php函数返回值和复杂表达式作为属性值。
一些属性用到了布尔值(true或false),它们表明为真或为假。如果没有为这些属性赋布尔值,那么默认使用true为其值。
例 3-3.函数属性语法
{include file="header.tpl"} {include file="header.tpl" nocache} // 等于nocache=true {include file="header.tpl" attrib_name="attrib value"} {include file=$includeFile} {include file=#includeFile# title="My Title"} {assign var=foo value={counter}} // plugin result {assign var=foo value=substr($bar,2,5)} // PHP function result {assign var=foo value=$bar|strlen} // 使用调节器 {assign var=foo value=$buh+$bar|strlen} // 更复杂的表达式 {html_select_date display_days=true} {mailto address="smarty@example.com"} <select name="company_id"> {html_options options=$companies selected=$company_id} </select>
|
虽然Smarty能处理一些复杂的表达式和语法,但从经验上来说的,一个好的做法是最低限度使用模版语法,将其专注于表现外在内容。如果发现你的模版语法太复杂,最好将与外在表现无关的后台处理通过插件或调节器交给php处理。