模板 -- 基础语法

所有的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.

Example 3-3. function attribute syntax

例 3-3.函数属性语法

{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`"}












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值