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>
Smarty函数
每一个smarty标签输出一个变量或者调用某种函数.
在定界符内 函数(用’{‘包住)和其属性(用界符包住)将被处理和输出.例如:
{funcname attr1=”val” attr2=”val”}.
{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属性
大多数函数都带有自己的属性以便于明确说明或者修改他们的行为.
smarty函数的属性很像HTML中的属性.
静态数值不需要加引号,但是字符串建议使用引号.
如果用变量作属性,它们也不能加引号.
一些属性用到了布尔值(真或假).
它们不需要加引号,可以是true,on,yes或者false,off,no.
Smarty属性用法
{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>
Smarty可以识别嵌入在双引号中的变量,只要此变量只包含数字、字母、下划线和中括号[].对于其他的符号(句号、对象相关的,等等)此变量必须用两个'`'(此符号和‘ ~ '在同一个键上,一般在ESC键下面一个键上)包住。
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
数学运算
{$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`"}
Smarty变量
内容列表
$template_dir [模板目录变量]
[从PHP分配的变量]
[从配置文件读取的变量]
[{$smarty} 保留变量]
Smarty有几种不同类型的变量.
变量 的类型取决于它的前缀是什么符号(或者被什么符号包围)
Smarty的变量可以直接被输出或者作为函数属性和修饰符(modifiers)的参数,或者用于内部的条件表达式等等.
如果要输出一个变量,只要用定界符将它括起来就可以.例如:
{$Name}
{$Contacts[row].Phone}
<body bgcolor="{#bgcolor#}">
调用从PHP分配的变量需在前加” ”符号.(译注:同php一样)调用模板内的assign函数分配的变量也是这样.(译注:也是用 加变量名来调用)
index.php:
$smarty = new Smarty;
$smarty->assign('firstname', 'Doug');
$smarty->assign('lastLoginDate', 'January 11th, 2001');
$smarty->display('index.tpl');
index.tpl:
Hello {$firstname}, glad to see you could make it.
<p>
Your last login was on {$lastLoginDate}.
OUTPUT:
Hello Doug, glad to see you could make it.
<p>
Your last login was on January 11th, 2001.
从配置文件引用的变量
foo.conf:
pageTitle = "This is mine"
bodyBgColor = "#eeeeee"
tableBorderSize = "3"
tableBgColor = "#bbbbbb"
rowBgColor = "#cccccc"
index.tpl:
{config_load file="foo.conf"}
<html>
<title>{#pageTitle#}</title>
<body bgcolor="{#bodyBgColor#}">
<table border="{#tableBorderSize#}" bgcolor="{#tableBgColor#}">
<tr bgcolor="{#rowBgColor#}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
index.tpl: (alternate syntax)
{config_load file="foo.conf"}
<html>
<title>{$smarty.config.pageTitle}</title>
<body bgcolor="{$smarty.config.bodyBgColor}">
<table border="{$smarty.config.tableBorderSize}" bgcolor="{$smarty.config.tableBgColor}">
<tr bgcolor="{$smarty.config.rowBgColor}">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>
OUTPUT: (same for both examples)
<html>
<title>This is mine</title>
<body bgcolor="#eeeeee">
<table border="3" bgcolor="#bbbbbb">
<tr bgcolor="#cccccc">
<td>First</td>
<td>Last</td>
<td>Address</td>
</tr>
</table>
</body>
</html>