smarty内建函数

来源:互联网 发布:阿里云 外网 iis 404 编辑:程序博客网 时间:2024/05/21 07:55

  1,capture
 capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面.
 任何在 {capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定.
 在模板中通过 $smarty.capture.foo 访问该变量.
 如果没有指定 name 属性,函数默认将使用 "default" 作为参数.
 {capture}必须成对出现,即以{/capture}作为结尾,该函数不能嵌套使用.

{* we don't want to print a table row unless content is displayed *}{* 该例在捕获到内容后输出一行包含数据的表格,如果没有捕获到就什么也不输出 *}{capture name=banner}{include file="get_banner.tpl"}{/capture}{if $smarty.capture.banner ne ""}<tr><td>{$smarty.capture.banner}</td></tr>{/if}

 

 


   2,config_load
 该函数用于从配置文件中加载变量
 配置文件有可能包含多个部分,此时可以使用附加属性 section 指定从哪一部分中取得变量.

 注意:配置文件中的 section 和模板内建函数 section 只是命名相同,毫不相干。

属性

类型

是否必须

缺省值

描述

file

string

Yes

n/a

待包含的配置文件的名称

section

string

No

n/a

配置文件中待加载部分的名称

scope

string

no

local

加载数据的作用域,取值必须为local, parent 或 global. local 说明该变量的作用域为当前模板. parent 说明该变量的作用域为当前模板和当前模板的父模板(调用当前模板的模板). global 说明该变量的作用域为所有模板.

global

boolean

No

No

说明加载的变量是否全局可见,等同于 scope=parent. 注意: 当指定了 scope 属性时,可以设置该属性,但模板忽略该属性值而以 scope 属性为准。

  

{config_load file="colors.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>

 

 

3,foreach ,foreachelse

属性类型是否必须缺省值描述fromstringYesn/a待循环数组的名称itemstringYesn/a当前处理元素的变量名称keystringNon/a

当前处理元素的键名

namestringNon/a该循环的名称,用于访问该循环

foreach 是除 section 之外处理循环的另一种方案(根据不同需要选择不同的方案).
foreach 用于处理简单数组(数组中的元素的类型一致),它的格式比 section 简单许多,缺点是只能处理简单数组.
foreach 必须和 /foreach 成对使用,且必须指定 fromitem 属性.
name 属性可以任意指定(字母、数字和下划线的组合).
foreach 可以嵌套,但必须保证嵌套中的 foreach 名称唯一.
from 属性(通常是数组)决定循环的次数.
foreachelse 语句在 from 变量没有值的时候被执行.

{* The key contains the key for each looped valueassignment looks like this:$smarty->assign("contacts", array(array("phone" => "1", "fax" => "2", "cell" => "3"), array("phone" => "555-4444", "fax" => "555-3333", "cell" => "760-1234")));*}{* 键就是数组的下标,请参看关于数组的解释 *}{foreach name=outer item=contact from=$contacts} {foreach key=key item=item from=$contact} {$key}: {$item}<br> {/foreach}{/foreach}OUTPUT:phone: 1<br>fax: 2<br>cell: 3<br>phone: 555-4444<br>fax: 555-3333<br>cell: 760-1234<br>

 

 

foreach 循环有自己的变量名,使用该变量名可以访问该循环. 使用方法为{$smarty.foreach.foreachname.varname},其中 foreachname 即在 foreach 中指定的name属性

 

iteration

iteration 用于显示当前循环的执行次数[待考]

iteration 总是从 1 开始,每执行一次增加 1.[待考]

first

当前 foreach 循环第一次执行时 first 被设置成 true.

last

当前 foreach 循环执行到最后一遍时 last 被设置成 true.

show

show 是 foreach 的一个参数. 取值为布尔值 true 或 false. 如果指定为 false 该循环不显示,如果循环指定了 foreachelse 子句,该子句显示与否也取决于show的取值.

total

total 用于显示循环执行的次数,可以在循环中或循环执行后调用.

 

 

 

4,include

属性类型是否必须缺省值描述filestringYesn/a待包含的模板文件名assignstringNon/a该属性指定一个变量保存待包含模板的输出[var ...][var type]Non/a传递给待包含模板的本地参数,只在待包含模板中有效

Include 标签用于在当前模板中包含其它模板. 当前模板中的变量在被包含的模板中可用. 必须指定 file 属性,该属性指明模板资源的位置.

You can optionally pass the assignattribute, which will specify a template variable name that the output ofincludewill be assigned to instead of displayed.

如果设置了 assign 属性,该属性对应的变量名用于保存待包含模板的输出,这样待包含模板的输出就不会直接显示了。

{include file="header.tpl"}{* body of template goes here *}{include file="footer.tpl"}

可以在属性中传递参数给待包含模板. 传递给待包含模板的参数只在待包含模板中可见. 如果传递的参数在待包含模板中有同名变量,那么该变量被传递的参数替代.

{include file="header.tpl" title="Main Menu" table_bgcolor="#c0c0c0"}{* body of template goes here *}{include file="footer.tpl" logo="http://my.domain.com/logo.gif"}

包含 $template_dir 文件夹之外的模板请使用 模板资源 说明的格式.

{* absolute filepath *}{include file="/usr/local/include/templates/header.tpl"}{* absolute filepath (same thing) *}{include file="file:/usr/local/include/templates/header.tpl"}{* windows absolute filepath (MUST use "file:" prefix) *}{include file="file:C:/www/pub/templates/header.tpl"}{* include from template resource named "db" *}{include file="db:header.tpl"}

 

原创粉丝点击