{capture}
{capture}
用于收集标签之间的模板输出并将其放入变量中,而不是将其显示出来。{capture name='foo'}
和 {/capture}
之间的任何内容都会收集到 name
属性中指定的变量中。
捕获的内容可以在模板中从变量 $smarty.capture.foo
中使用,其中“foo”是在 name
属性中传递的值。如果不提供 name
属性,则将使用“default”作为名称,即 $smarty.capture.default
。
{capture}'s
可以嵌套。
属性
属性名称 | 必需 | 描述 |
---|---|---|
名称 | 是 | 捕获块的名称 |
分配 | 否 | 要将捕获的输出分配到的变量名称 |
追加 | 否 | 要将捕获的输出追加到的数组变量的名称 |
选项标记
名称 | 描述 |
---|---|
取消缓存 | 禁用此捕获块的缓存 |
实例
{* we don't want to print a div tag unless content is displayed *}
{capture name="banner"}
{capture "banner"} {* short-hand *}
{include file="get_banner.tpl"}
{/capture}
{if $smarty.capture.banner ne ""}
<div id="banner">{$smarty.capture.banner}</div>
{/if}
此示例演示了捕获函数。
{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>
此示例还演示了如何使用多次捕获调用来创建包含捕获内容的数组。
{capture append="foo"}hello{/capture}I say just {capture append="foo"}world{/capture}
{foreach $foo as $text}{$text} {/foreach}
以上示例将输出
还请参阅 $smarty.capture
、{eval}
、{fetch}
、fetch()
和 {assign}
。