Chapter 7. Built-in Functions[第七章.内建函数]

Table of Contents
capture
config_load
foreach,foreachelse
include
include_php
insert
if,elseif,else
ldelim,rdelim
literal
php
section,sectionelse
strip

Smarty comes with several built-in functions. Built-in functions are integral to the template language. You cannot create custom functions with the same names, nor can you modify built-in functions.

Smarty自带一些内建函数.
内建函数是模板语言的一部分.
用户不能创建名称和内建函数一样的自定义函数,也不能修改内建函数.

capture

Attribute NameTypeRequiredDefaultDescription
namestringnodefaultThe name of the captured block
assignstringNon/aThe variable name where to assign the captured output to

属性 类型 是否必须 缺省值 描述
name string no default 数据采集区域名称
assign string No n/a 数据采集区域在哪分配给变量name[待考]

capture is used to collect the output of the template into a variable instead of displaying it. Any content between {capture name="foo"} and {/capture} is collected into the variable specified in the name attribute. The captured content can be used in the template from the special variable $smarty.capture.foo where foo is the value passed in the name attribute. If you do not supply a name attribute, then "default" will be used. All {capture} commands must be paired with {/capture}. You can nest capture commands.

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

Technical Note: Smarty 1.4.0 - 1.4.4 placed the captured content into the variable named $return. As of 1.4.5, this behavior was changed to use the name attribute, so update your templates accordingly.

技术提示:
Smarty 1.4.0 - 1.4.4 版把捕获到的内容存储于 $return 变量里.
在 1.4.5 版以后就将该数据保存到了由 name 属性指定的变量里.请升级你的模板.

Caution

Be careful when capturing {insert} output. If you have caching turned on and you have {insert} commands that you expect to run within cached content, do not capture this content.


警告

当希望捕获包含 {insert} 命令的数据时要特别注意. 如果打开了缓存并希望将 {insert} 命令输出到缓存中,不要捕获该区域的数据.

Example 7-1. capturing template content
例 7-1.捕获模板内容

{* 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}