Comunitatea PHP Romania
 

 
Modifiers

Modifiers

Modifiers are little functions that are applied to a variable in the template before it is displayed or used in some other context. Modifiers can be chained together.

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

The first parameter to the modifier plugin is the value on which the modifier is to operate. The rest of the parameters are optional, depending on what kind of operation is to be performed.

The modifier has to return the result of its processing.

Example 16-3. A simple modifier plugin

This plugin basically aliases one of the built-in PHP functions. It does not have any additional parameters.

<?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. More complex modifier plugin

<?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($string0$length+1);
        if (
$break_words)
            
$fragment substr($fragment0, -1);
        else
            
$fragment preg_replace('/\s+(\S+)?$/'''$fragment);
        return 
$fragment.$etc;
    } else
        return 
$string;
}
?>

See also register_modifier(), unregister_modifier().

Ultimele discutii in forum RSS Forum

Ultimele articole Ultimele articole

Top membri

Pirahna Pirahna
la birou
carco carco
Bucuresti
Birkoff Birkoff
Bucuresti
mihaitha mihaitha
Sibiu
Mascka Mascka
Braila
gabysolomon gabysolomon
Bacau
whooper whooper
Toronto ON
raul_ raul_
dechim dechim
Drobeta Turnu Severin
Amenthes Amenthes

Newsletter


Email:
 inscriere
 renuntare
 
 Arhiva newsletter

Parteneriat

Copyright © 2001-2008 PHP Romania Add PHPRomania to Google Add PHPRomania to Del.icio.us Add PHPRomania to Stumbleupon Add PHPRomania to Yahoo! Add PHPRomania to Digg Add PHPRomania to Blink French Grammar | Unsecured Loans | Mortgage | Mercedes Benz | Cingular Ringtones
Ads: Partener Way2Web Nework: gazduire web | inregistrare domenii | web design | imobiliare | web hosting
Powered by Simplis