What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
大多数函数 都会使用属性来定义或者修改它们的行为。 Smarty函数中的属性比较像HTML语法中的属性。 静态值不需要引号引起来,但必须是纯字符串。 带或不带修饰器的变量都可以使用,而且也不需要引号, 甚至可以使用PHP函数的结果,插件结果和复杂的表达式。
一些属性要求布尔值(TRUE
或 FALSE
)。
它们可以直接写成true
和 false
。
如果属性没有被赋值,那么它会将true作为默认值。
Example 3.4. 函数属性语法
{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}} // 插件结果 {assign var=foo value=substr($bar,2,5)} // PHP函数结果 {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里, 然后在模板中通过插件或者修饰器来调用。