<table summary="Header navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><th colspan="3" align="center">Smarty - the compiling PHP template engine</th></tr><tr><td width="25%" align="left" valign="bottom"><a href="language.function.call.html" accesskey="P">Prev</a></td> <td width="50%" align="center" valign="bottom">Chapter 7. Built-in Functions[第七章.内置函数]</td> <td width="25%" align="right" valign="bottom"><a href="language.function.config.load.html" accesskey="N">Next</a></td></tr></table> # {capture}捕获 {capture} is used to collect the output of the template between the tags 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 variable $smarty.capture.foo where “foo” is the value passed in the name attribute. If you do not supply the name attribute, then “default” will be used as the name ie $smarty.capture.default. {capture}'s can be nested. {capture}用来捕获模板输出的数据并将其存储到一个变量里,而不是将它们输出到页面。任何在{capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由*name*属性指定。 在模板中,可以通过$smarty.capture.foo访问{capture}内容,‘foo’是传递给*name*属性的值。如果没有提供name属性,函数默认将使用 "default" 作为参数,例如$smarty.capture.default。 {capture}可以嵌套。 **Attribute:** | Attribute Name | Type | Required | Default | Description | |-----|-----|-----|-----|-----| | name | string | yes | *default* | The name of the captured block | | assign | string | No | *n/a* | The variable name where to assign the captured output to | | append | string | no | n/a | The name of an array variable where to append the captured output to | **Option Flags: ** | Name | Description | |-----|-----| | nocache | Disables caching of this captured block | **属性:** | 属性 | 类型 | 是否必须 | 缺省值 | 描述 | |-----|-----|-----|-----|-----| | name | string | yes | *default* | 数据捕获区域名称 | | assign | string | no | *n/a* | 变量名,用来指定捕获的输出分配到哪里 | | append | string | no | n/a | 数组变量,用来声明捕获的输出(存贮)追加到哪里 | **选项标记: ** | 名称 | 描述 | |-----|-----| | nocache | 捕获的区域禁止缓存 | <table width="80%" border="0" align="center" cellpadding="2" cellspacing="2" class="warning"><th>警 告 </th> <tr><td><p>Caution<br/> Be careful when capturing {insert} output. If you have $caching enabled and you have {insert} commands that you expect to run within cached content, do not capture this content.</p> <p>当捕获<a href="language.function.insert.html">{insert}</a>输出时要非常小心!如果你开启了$caching缓存,希望在缓存内容里运行{insert}命令。那么,请不要捕获{insert}里面的内容!因为{insert}内容总是不被缓存。</p></td> </tr></table> <table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN721" id="AEN721"> </a> <b>Example 7.21. {capture} with the name attribute<br/> 例 7-21.{capture}的name属性</b> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING">{* we don't want to print a div tag unless content is displayed *}{* 我们不想输出一个div标签,除非包含的内容是无法显示的 *}{capture name="banner"}{capture "banner"} {* short-hand *} {include file="get_banner.tpl"}{/capture}{if $smarty.capture.banner ne ""}&lt;div id="banner"&gt;{$smarty.capture.banner}&lt;/div&gt;{/if}</pre> </td> </tr></table></div></td> </tr></table> **Example 7.22. {capture} into a template variable** **例 7-22.{capture}内容分配给变量** | ~~~ This example demonstrates演示 the capture function.{capture name=some_content assign=popText}{capture some_content assign=popText} {* short-hand *}The server is {$my_server_name|upper} at {$my_server_addr}<br>Your ip is {$my_ip}.{/capture}<a href="#">{$popText}</a> ~~~ | |-----| **Example 7.23. {capture} into a template array variable** **例 7-23.捕获模板内容** | ~~~ This example also demonstrates how multiple calls of capture can be used to create an array with captured content. 这个示例演示了多重调用capture是如何利用捕获的内容组成一个数组的。{capture append="foo"}hello{/capture}I say just {capture append="foo"}world{/capture}{foreach $foo as $text}{$text} {/foreach} The above example will output:I say just hello world ~~~ | |-----| See also $smarty.capture, {eval}, {fetch}, fetch() and {assign}. 参考[$smarty.capture](#)、[{eval}](#)、[{fetch}](#)、[fetch()](#)和[{assign}](#)。 <table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="language.function.call.html" accesskey="P">Prev</a></td><td width="34%" align="center" valign="top"><a href="index.html" accesskey="H">Home</a></td><td width="33%" align="right" valign="top"><a href="language.function.config.load.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">{call}<br/> 调用</td><td width="34%" align="center" valign="top"><a href="language.builtin.functions.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">{config_load}<br/> 加载配置</td></tr></table>