目录搜索
Smarty模板编译引擎I.开始第一章. 什么是Smarty?第二章. 安装要求基本安装扩展设置II.模板设计者篇第三章.基本语法注释函数属性第四章.变量从PHP分配的变量Associative arraysArray indexesObjects从配置文件读取的变量{$smarty}保留变量Request variables{$smarty.now}{$smarty.const}{$smarty.capture}{$smarty.config}{$smarty.section}{$smarty.template}第五章.变量调节器capitalizecount_characterscatcount_paragraphscount_sentencescount_wordsdate_formatdefaultescapeindentlowernl2brregex_replacereplacespacifystring_formatstripstrip_tagstruncateupperwordwrap第六章.组合修改器第七章.内建函数captureconfig_loadforeachincludeinclude_phpinsertifldelimliteralphpsectionindexindex_previndex_nextiterationfirstlastrownumloopshowtotalstrip第八章.自定义函数assigncountercycledebugevalfetchhtml_checkboxeshtml_imagehtml_optionshtml_radioshtml_select_datehtml_select_timehtml_tablemathmailtopopup_initpopuptextformat第九章.配置文件第十章.调试控制台III.模板程序员篇第十一章 常量SMARTY_DIR第十二章 变量$template_dir$compile_dir$config_dir$plugins_dir$debugging$debug_tpl$debugging_ctrl$global_assign$undefined$autoload_filters$compile_check$force_compile$caching$cache_dir$cache_lifetime$cache_handler_func$cache_modified_check$config_overwrite$config_booleanize$config_read_hidden$config_fix_newlines$default_template_handler_func$php_handling$security$secure_dir$security_settings$trusted_dir$left_delimiter$right_delimiter$compiler_class$request_vars_order$compile_id$use_sub_dirs$default_modifiers$default_resource_type第十三章.方法appendappend_by_refassignassign_by_refclear_all_assignclear_all_cacheclear_assignclear_cacheclear_compiled_tplclear_configconfig_loaddisplayfetchget_config_varsget_registered_objectget_template_varsis_cachedload_filterregister_blockregister_compiler_functionregister_functionregister_modifierregister_objectregister_outputfilterregister_postfilterregister_prefilterregister_resourcetrigger_errortemplate_existsunregister_blockunregister_compiler_functionunregister_functionunregister_modifierunregister_objectunregister_outputfilterunregister_postfilterunregister_prefilterunregister_resource第十四章.缓存Setting Up CachingMultiple Caches Per PageCache GroupsControlling Cacheability of Plugins' Output第十五章.高级特点ObjectsPrefiltersPostfiltersOutput FiltersCache Handler FunctionResourcesTemplates from $template_dirTemplates from any directoryTemplates from other sourcesDefault template handler function第十六章.以插件扩展SmartyHow Plugins WorkNaming ConventionsWriting PluginsTemplate FunctionsModifiersBlock FunctionsCompiler FunctionsPrefilters/PostfiltersOutput FiltersResourcesInsertsⅣ.高级特点第十七章.疑难解答Smarty/PHP errors第18章.使用技巧和经验Blank Variable HandlingDefault Variable HandlingPassing variable title to header templateDatesWAP/WMLComponentized TemplatesObfuscating E-mail Addresses第十九章. 相关资源第二十章. 漏洞
文字

Block Functions块函数

void smarty_block_ name (array $params, mixed $content, object &$smarty)

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}.
块函数的形式是这样的:{func} .. {/func}。换句话说,他们用标记圈起一个块,然后对这个块的内容进行操作。块函数优先于同名的传统函数,即你不能同时有通明的传统函数{func}和块函数{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 how to change this).
默认地你的函数执行被Smarty调用两次:一次是在开始标记,另一次是在结束标记(参考下面的&$repeat怎样改变这种情况)

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 as an associative array. You can either access those values directly, e.g. $params['start'] or use extract($params) to import them into the symbol table. The opening tag attributes are also accessible to your function when processing the closing tag.
块函数仅开始标记可以有属性。所有从模板传替给模板函数的属性被囊括与一个集合数组参数中。你可以直接获取其值,例如:$params['start']或者是用extract($params)将它们导入符号表中。当处理结束标记时,开始标记的属性对你的函数也是可用的。

The value of $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.
变量 $content 的值取决于是否因开始标记或结束标记调用你的函数。假如是开始标记,它会是空的,如果是结束标记,它会是模板块的内容。请注意模板块已经被Smarty处理,所以你接收到的结果是输出后的模板而不是原样模板。

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 block 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 .
参数 &$repeat 通过参考引用传递给函数执行过程并为其提供一个可能值来控制显示块多少遍。默认情况下在首次调用块函数(块开始标记)时变量 $repeat 是真,在随后的所有块函数调用中其始终是假。每当函数执行返回的 &$repeat 是真时,在{func} .. {/func}之间的内容再次求值,函数执行接收一个新块参数 $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() on it and the structure should be apparent.
如果你嵌套了块函数,通过访问变量$smarty->_tag_stack 找出父块函数是可能的。仅仅对块函数运行一下var_dump(),函数结构就会一目了然了。

See also: register_block(), unregister_block().

Example 16-5. block function

<?php

function smarty_block_translate($params, $content, &$smarty)
{
 if (isset($content)) {
 $lang = $params['lang'];
 // do some intelligent translation thing here with $content
 return $translation;
 }
}
上一篇:下一篇: