所有的smarty模板标签都被加上了定界符.
默认情况下是 { 和},但它们是可被改变的.
例如,我们假定你在使用默认定界符.
在smarty里,所有定界符以外的内容都是静态输出的,或者称之为不可改变.
当smarty遇到了模板标签,将尝试解释他们,然后再以恰当的方式输出 .
1、Comments[注释]
模板注释被*号包围,例如 {* this is a comment *}
smarty注释不会在模板文件的最后输出中出现.
它只是模板内在的注释.
{* Smarty *}
{* include the header file here *}
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{* display dropdown lists *}
<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>
2、Functions[函数]
每一个smarty标签输出一个变量或者调用某种函数.
在定界符内 函数(用'{'包住)和其属性(用界符包住)将被处理和输出.例如:
{funcname attr1="val" attr2="val"}.
例 3-2.函数语法
{config_load file="colors.conf"}
{include file="header.tpl"}
{if $highlight_name}
Welcome, <font color="{#fontColor#}">{$name}!</font>
{else}
Welcome, {$name}!
{/if}
{include file="footer.tpl"}
在模板里无论是内建函数还是自定义函数都有相同的语法.
内建函数将在smarty内部工作,例如 {if} , {section} and {strip} .他们不能被修改.
自定义函数通过插件机制起作用,它们是附加函数. 只要你喜欢,可以随意修改.你也可以自行添加.
例如 {html_options} 和 {html_select_date}
3、Attributes[属性]
大多数函数都带有自己的属性以便于明确说明或者修改他们的行为.
smarty函数的属性很像HTML中的属性.
静态数值不需要加引号,但是字符串建议使用引号.
如果用变量作属性,它们也不能加引号.
Some attributes require boolean values (true or false). These can be specified as either unquoted true, on, and yes, or false, off, and no.
一些属性用到了布尔值(真或假).
它们不需要加引号,可以是true,on,yes或者false,off,no.
{include file="header.tpl"}
{include file=$includeFile}
{include file=#includeFile#}
{html_select_date display_days=yes}
<SELECT name=company>
{html_options values=$vals selected=$selected output=$output}
</SELECT>
4、Embedding Vars in Double Quotes[双引号里值的嵌入]
Smarty可以识别嵌入在双引号中的变量,只要此变量只包含数字、字母、下划线和中括号[].对于其他的符号(句号、对象相关的,等等)此变量必须用两个'`'(此符号和‘ ~ '在同一个键上,一般在ESC键下面一个键上)包住。
例 3-4.双引号里值的嵌入语法
SYNTAX EXAMPLES:
{func var="test $foo test"} <-- sees $foo
{func var="test $foo_bar test"} <-- sees $foo_bar
{func var="test $foo[0] test"} <-- sees $foo[0]
{func var="test $foo[bar] test"} <-- sees $foo[bar]
{func var="test $foo.bar test"} <-- sees $foo (not $foo.bar)
{func var="test `$foo.bar` test"} <-- sees $foo.bar
PRACTICAL EXAMPLES:
{include file="subdir/$tpl_name.tpl"} <-- will replace $tpl_name with value
{cycle values="one,two,`$smarty.config.myval`"} <-- must have backticks
5、Math[数学运算]
数学运算可以直接应用到变量。
例 3-5.数学运算的例子
{$foo+1}
{$foo*$bar}
{* some more complicated examples *}
{$foo->bar-$bar[1]*$baz->foo->bar()-3*7}
{if ($foo+$bar.test%$baz*134232+10+$b+10)}
{$foo|truncate:"`$fooTruncCount/$barTruncFactor-1`"}
{assign var="foo" value="`$foo+$bar`"}