W3Cschool
恭喜您成為首批注冊用戶
獲得88經驗值獎勵
{capture}
可以捕獲標記范圍內的輸出內容,存到變量中而不顯示。 任何在{capture name='foo'}
和 {/capture}
之間的內容都會被捕獲到變量,變量名可以通過name
屬性來指定。
捕獲的內容可以通過$smarty.capture.foo
變量來使用,這里foo是設置的name
屬性。 如果沒有提供name
屬性,默認是“default”,也就是 $smarty.capture.default
.
{capture}
可以被嵌套使用。
屬性:
參數名稱 | 類型 | 必選參數 | 默認值 | 說明 |
---|---|---|---|---|
name | string | Yes | n/a | 捕獲區(qū)域的名稱 |
assign | string | No | n/a | 捕獲內容后賦值的變量名 |
append | string | No | n/a | 將捕獲的內容增加到數組中 |
可選標記:
名稱 | 說明 |
---|---|
nocache | 關閉捕獲區(qū)域的緩存 |
當捕獲{insert}
輸出的時候請小心。 如果開啟了$caching
并且 你希望通過{insert}
在緩存的頁面上顯示動態(tài)內容,那么你無法捕獲這些內容。
Example 7.21. {capture}使用name屬性
{* 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}
Example 7.22. {capture} 捕獲內容到變量
下面是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
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: