Revision 148 | Blame | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed
Block Functions {#plugins.block.functions}===============voidsmarty\_block\_namearray\$paramsmixed\$contentobject\$templateboolean&\$repeatBlock functions are functions of the form: `{func} .. {/func}`. In otherwords, they enclose a template block and operate on the contents of thisblock. Block functions take precedence over [customfunctions](#language.custom.functions) of the same name, that is, youcannot 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.)- Starting with Smarty 3.1 the returned value of the opening tag callis displayed as well.- Only the opening tag of the block function may have[attributes](#language.syntax.attributes). All attributes passed totemplate functions from the template are contained in the `$params`variable as an associative array. The opening tag attributes arealso accessible to your function when processing the closing tag.- The value of the `$content` variable depends on whether yourfunction is called for the opening or closing tag. In case of theopening tag, it will be NULL, and in case of the closing tag it willbe the contents of the template block. Note that the template blockwill have already been processed by Smarty, so all you will receiveis the template output, not the template source.- The parameter `$repeat` is passed by reference to the functionimplementation and provides a possibility for it to control how manytimes the block is displayed. By default `$repeat` is TRUE at thefirst call of the block-function (the opening tag) and FALSE on allsubsequent calls to the block function (the block\'s closing tag).Each time the function implementation returns with `$repeat` beingTRUE, the contents between `{func}...{/func}` are evaluated and thefunction implementation is called again with the new block contentsin the parameter `$content`.If you have nested block functions, it\'s possible to find out what theparent block function is by accessing `$smarty->_tag_stack` variable.Just do a [`var_dump()`](https://www.php.net/var_dump) on it and thestructure should be apparent.<?php/** Smarty plugin* -------------------------------------------------------------* File: block.translate.php* Type: block* Name: translate* Purpose: translate a block of text* -------------------------------------------------------------*/function smarty_block_translate($params, $content, Smarty_Internal_Template $template, &$repeat){// only output on the closing tagif(!$repeat){if (isset($content)) {$lang = $params['lang'];// do some intelligent translation thing here with $contentreturn $translation;}}}?>See also: [`registerPlugin()`](#api.register.plugin),[`unregisterPlugin()`](#api.unregister.plugin).