Get Smarty

Donate

Donate Bitcoin Bitcoin
Paypal

Smarty Icon

You may use the Smarty logo according to the trademark notice.

Smarty Template Engine Smarty Template Engine

For sponsorship, advertising, news or other inquiries, contact us at:

Sites Using Smarty

Largest classified with thousands of ads at Shoppok

Buy cheap eyeglasses from Cheapglasses123.com and save up to 80%.

Buy prescription glasses from www.australiaglasses.com and save.

Cheap Glasses Now On Sale at GlassesPeople.com. Starts At $7.95.

Where to buy discount wedding dresses and cheap smart dresses free shipping - Weddingdresstrend.com

Find Wedding Dresses Online at Canada from Ca-dresses.com

Find your local domestic cleaner

Brautkleider auf Topwedding.de

Best Wedding Dresses Australia at AuBridalDresses.com

Find free files to download on allwhatyouwant.net

Advertisement

Block Functions

void smarty_block_name( $params,  
  $content,  
  $template,  
  &$repeat);  
array $params;
mixed $content;
object $template;
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.)

  • Starting with Smarty 3.1 the returned value of the opening tag call is displayed as well.

  • 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() on it and the structure should be apparent.

Example 18.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_Internal_Template $template, &$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;
        }
    }
}
?>

     

See also: registerPlugin(), unregisterPlugin().

Comments
No comments for this page.
Post a Comment
All comments are moderated. Support questions are ignored, use the forums instead.
Author:
Email: (not shown)
What is 18 plus 5? (Are you human?)

Advertisement

Sponsors [info]

UK Web Hosting @webhost.uk.net
Best Web Hosting @rshosting.com
Web Hosting UK @webhostinguk.com
Unlimited Web Hosting @infrenion.com
App Entwicklung @morphodo.com
GoDaddy Coupon @buy-hosting.net
First Click Internet Marketing @fcinternetmarketing.com