<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="plugins.modifiers.html" accesskey="P">Prev</a></td><td width="50%" align="center" valign="bottom">Chapter 16. Extending Smarty With Plugins</td><td width="25%" align="right" valign="bottom"><a href="plugins.compiler.functions.html" accesskey="N">Next</a></td></tr></table>
# Block Functions[塊函數]
> void smarty_block_name($params, $content, &$smarty, &$repeat);
array $params;
mixed $content;
object &$smarty;
boolean &$repeat;
Block functions are functions of the form: {func} .. {/func}. In other words, they enclose a template block and operate on the contents of this block. Block functions take precedence over custom functions of the same name, that is, you cannot have both custom function {func} and block function {func}..{/func}.
By default your function implementation is called twice by Smarty: once for the opening tag, and once for the closing tag. (See $repeat below on how to change this.)
Only the opening tag of the block function may have attributes. All attributes passed to template functions from the template are contained in the $params variable as an associative array. The opening tag attributes are also accessible to your function when processing the closing tag.
The value of the $content variable depends on whether your function is called for the opening or closing tag. In case of the opening tag, it will be NULL, and in case of the closing tag it will be the contents of the template block. Note that the template block will have already been processed by Smarty, so all you will receive is the template output, not the template source.
The parameter $repeat is passed by reference to the function implementation and provides a possibility for it to control how many times the block is displayed. By default $repeat is TRUE at the first call of the block-function (the opening tag) and FALSE on all subsequent calls to the block function (the block's closing tag). Each time the function implementation returns with $repeat being TRUE, the contents between {func}...{/func} are evaluated and the function implementation is called again with the new block contents in the parameter $content.
If you have nested block functions, it's possible to find out what the parent block function is by accessing $smarty->_tag_stack variable. Just do a var_dump() [http://php.net/var_dump] on it and the structure should be apparent.
塊函數的形式是這樣的:{func} .. {/func}。換句話說,他們被封閉在一個模板區域內,然后對該區域的內容進行操作。塊函數優先于同名的自定義函數,即是說,你不能同時使用自定義函數{func}和塊函數{func} .. {/func}。
默認地,你的函數實現會被Smarty調用兩次:一次是在開始標簽,另一次是在閉合標簽(參考下面的&$repeat關于怎樣改變這種設置)。
只有塊函數的開始標簽具有屬性。所有屬性包含在作為關聯數組的$params變量中,經由模板傳遞給模板函數。當處理閉合標簽時,函數同樣可訪問開始標簽的屬性。
$content變量值取決于你的函數是被開始標簽調用還是被閉合標簽調用。假如是開始標簽,變量值將為NULL,如果是閉合標簽,$content變量值為模板塊的內容。請注意這時模板塊已經被Smarty處理過,因此你所接收到的是模板的輸出而不是模板資源。
&$repeat參數通過引用傳遞給函數執行,并為其提供控制塊顯示多少次的可能性。默認情況下,在首次調用塊函數(塊開始標簽)時&$repeat變量為true,在隨后的所有塊函數(閉合標簽)調用中其值始終為false。函數每次執行返回的&$repeat值為true時,{func} .. {/func}之間的內容會被求值,同時參數$content里的新塊內容會再次調用執行函數(譯注:運行方法有點類似遞歸函數)。
如果你嵌套了塊函數,可以通過$smarty->_tag_stack變量訪問找出父塊函數。只須對塊函數運行一下[var_dump()](http://php.net/var_dump),函數結構就可以一目了然了。
<table width="100%" border="0" cellpadding="0" cellspacing="0" class="EXAMPLE"><tr><td><div class="EXAMPLE"> <a name="AEN4165" id="AEN4165"> </a> <b>Example 16.5. block function</b> <br/><strong>例16-5.塊函數 </strong> <table border="0" bgcolor="#E0E0E0" width="100%"><tr><td><pre class="PROGRAMLISTING"><?php/* * Smarty plugin * ------------------------------------------------------------- * File: block.translate.php * Type: block * Name: translate * Purpose: translate a block of text * ------------------------------------------------------------- */function smarty_block_translate($params, $content, $smarty, &$repeat){ // only output on the closing tag if(!$repeat){ if (isset($content)) { $lang = $params['lang']; // do some intelligent translation thing here with $content return $translation; } }}?></pre></td></tr></table><p> 參見<a href="api.registerplugin.html">registerPlugin()</a>、<a href="api.unregisterplugin.html">unregisterPlugin()</a>。</p></div></td></tr></table>
<table summary="Footer navigation table" width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td width="33%" align="left" valign="top"><a href="plugins.modifiers.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="plugins.compiler.functions.html" accesskey="N">Next</a></td></tr><tr><td width="33%" align="left" valign="top">Modifiers<br/> 調節器</td><td width="34%" align="center" valign="top"><a href="plugins.html" accesskey="U">Up</a></td><td width="33%" align="right" valign="top">Compiler Functions<br/> 編譯函數</td></tr></table>
- Smarty模板編譯引擎
- 序
- 譯序
- I.開始
- 第一章. 什么是Smarty?
- 第二章. 安裝
- II.模板設計者篇
- 第三章.基本語法
- 注釋
- 變量
- 函數
- 屬性
- 雙引號里嵌入變量
- 數學運算
- 忽略Smarty解析
- 第四章.變量
- 從PHP分配的變量
- 從配置文件讀取的變量
- 變量范圍
- {$smarty}保留變量
- 第五章.變量調節器
- capitalize
- cat
- count_characters
- count_paragraphs
- count_sentences
- count_words
- date_format
- default
- escape
- indent
- lower
- nl2br
- regex_replace
- replace
- spacify
- string_format
- strip
- strip_tags
- truncate
- upper
- wordwrap
- 第六章.組合修改器
- 第七章.內置函數
- {$var=}
- {append}
- {assign}
- {block}
- {call}
- {capture}
- {config_load}
- {debug}
- {extends}
- {for}
- {foreach},{foreachelse}
- @index
- {function}
- {if},{elseif},{else}
- {include}
- {include_php}
- {insert}
- {ldelim},{rdelim}
- {literal}
- {nocache}
- {php}
- {section},{sectionelse}
- .index
- {while}
- 第八章.自定義函數
- {counter}
- {cycle}
- {eval}
- {fetch}
- {html_checkboxes}
- {html_image}
- {html_options}
- {html_radios}
- {html_select_date}
- {html_select_time}
- {html_table}
- {mailto}
- {math}
- {textformat}
- 第九章.配置文件
- 第十章.調試控制臺
- III.模板程序員篇
- 第十一章 常量
- SMARTY_DIR
- 第十二章 Smarty類變量
- $template_dir
- 第十三章.Smarty類方法
- append()
- appendByRef()
- assign()
- assignByRef()
- clearAllAssign()
- clearAllCache()
- clearAssign()
- clearCache()
- clearCompiledTpl()
- clearConfig()
- compileAllConfig()
- compileAllTemplates()
- configLoad()
- createData()
- createTemplate()
- disableSecurity()
- display()
- enableSecurity()
- fetch()
- getConfigVars()
- getRegisteredObject()
- getTags()
- getTemplateVars()
- isCached()
- loadFilter()
- registerFilter()
- registerPlugin()
- registerObject()
- registerResource()
- templateExists()
- unregisterFilter()
- unregisterPlugin()
- unregisterObject()
- unregisterResource()
- testInstall()
- 第十四章.緩存
- 建立緩存
- 多重緩存
- 緩存集合
- 控制插件輸出的可緩存性
- 第十五章.高級特性
- 安全
- 通過模板更改設置
- 模板繼承
- 數據流
- 對象
- 靜態類
- 預過濾器
- 后過濾器
- 輸出過濾器
- 緩存處理函數
- 資源
- 第十六章.以插件擴展Smarty
- 插件如何工作
- 命名約定
- 編寫插件
- 模板函數
- 調節器
- 塊函數
- 編譯函數
- 預濾器/后濾器
- 輸出過濾器
- 資源
- 插入
- Ⅳ.附錄
- 第十七章.疑難解答
- Smarty/PHP 錯誤
- 第十八章.使用技巧和經驗
- 空白變量處理
- 默認變量處理
- 傳遞變量標題給頭模板
- 日期
- WAP/WML
- 組件化模板
- 拒絕電子郵件地址
- 第十九章. 相關資源
- 第二十章. 漏洞
- 3.0安裝包
- 2.x版本升級至3.x版本的提示
- 3.0.x使用指南
- 翻譯人員列表