What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
{capture}
は、タグの間のテンプレートの出力を集め、
それをブラウザに表示する代わりに変数に受け渡します。
{capture name='foo'}
と {/capture}
の間のあらゆるコンテンツは、name
属性で指定した変数に格納されます。
キャプチャされたコンテンツは、特別な変数
$smarty.capture.foo
(“foo” は name
属性で指定した変数) によって利用できます。
name
属性を指定しない場合は “default”
が使われ、$smarty.capture.default
のようになります。
{capture}'s
はネスト可能です。
属性
属性名 | 型 | 必須 | デフォルト | 概要 |
---|---|---|---|---|
name | string | Yes | n/a | キャプチャされるブロックの名前 |
assign | string | No | n/a | キャプチャされた出力を割り当てるための変数名 |
append | string | No | n/a | キャプチャされた出力を追記するための配列変数名 |
オプションのフラグ
名前 | 概要 |
---|---|
nocache | キャプチャされたブロックのキャッシュを無効にする |
{insert}
の出力をキャプチャする際には注意が必要です。
$caching
が有効の時に、実行したい
{insert}
コマンドがもしキャッシュされたコンテンツ内にあるのなら、そのコンテンツはキャプチャされません。
Example 7.21. name 属性を使用した {capture}
{* コンテンツが表示されない限り、テーブルの行を表示しません *} {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}
Example 7.22. {capture} をテンプレート変数に格納
この例は、キャプチャ関数の使用法を示すものです。
{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} をテンプレート配列変数に格納
この例は、キャプチャを複数回行って、その結果を配列に格納する方法を示すものです。
{capture append="foo"}hello{/capture}I say just {capture append="foo"}world{/capture} {foreach $foo as $text}{$text} {/foreach}
上の例の出力は次のようになります。
I say just hello world
$smarty.capture
、
{eval}
、
{fetch}
、
fetch()
および {assign}
も参照ください。