Get Smarty

Donate

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

Advertisement

Modificatori

I modificatori sono piccole funzioni che vengono applicate ad una variabile del template prima che venga visualizzata o usata in qualche altro contesto. I modificatori possono essere concatenati.

mixed smarty_modifier_name( $value,  
  $param1);  
mixed $value;
[mixed $param1, ...];
 

Il primo parametro passato al plugin modificatore è il valore sul quale il modificatore stesso deve operare. Gli altri parametri possono essere opzionali, a seconda del tipo di operazione che deve essere eseguita.

Il modificatore deve restituire il risultato della sua esecuzione.

Vedere anche register_modifier(), unregister_modifier().

Example 16.3. un semplice plugin modificatore

Questo plugin fondamentalmente crea un sinonimo per una delle funzioni incorporate in PHP. Non prevede parametri aggiuntivi.


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.capitalize.php
 * Type:     modifier
 * Name:     capitalize
 * Purpose:  capitalize words in the string
 * -------------------------------------------------------------
 */
function smarty_modifier_capitalize($string)
{
    return ucwords($string);
}
?>


Example 16.4. un plugin modificatore più complesso


<?php
/*
 * Smarty plugin
 * -------------------------------------------------------------
 * File:     modifier.truncate.php
 * Type:     modifier
 * Name:     truncate
 * Purpose:  Truncate a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and 
 *           appending the $etc string.
 * -------------------------------------------------------------
 */
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                  $break_words = false)
{
    if ($length == 0)
        return '';

    if (strlen($string) > $length) {
        $length -= strlen($etc);
        $fragment = substr($string, 0, $length+1);
        if ($break_words)
            $fragment = substr($fragment, 0, -1);
        else
            $fragment = preg_replace('/\s+(\S+)?$/', '', $fragment);
        return $fragment.$etc;
    } else
        return $string;
}
?>

     

Comments
No comments for this page.

Advertisement

Sponsors [info]

Sponsors