The OpenNET Project / Index page

[ новости /+++ | форум | теги | ]

Каталог документации / Раздел "PHP" / Оглавление документа

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}.

Your function implementation is called twice by Smarty: once for the opening tag, and once for the closing tag.

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.

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.

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.

See also: register_block(), unregister_block().

Пример 16-5. block function

<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     block.translate.php
 * Type:     block
 * Name:     translate
 * Purpose:  translate a block of text
 * -------------------------------------------------------------
 */
function smarty_block_translate($params, $content, &$smarty)
{
    if ($content) {
        $lang = $params['lang'];
        // do some intelligent translation thing here with $content
        echo $translation;
    }
}



Партнёры:
PostgresPro
Inferno Solutions
Hosting by Hoster.ru
Хостинг:

Закладки на сайте
Проследить за страницей
Created 1996-2024 by Maxim Chirkov
Добавить, Поддержать, Вебмастеру