Search code examples
phpdynamicsmartyondemandmodifiers

How to dynamicly apply variable modifiers in Smarty 2.x


I can't find the solution of applying modifiers dynamicly in Smarty.

Template - I would like to work this way (example)

{$myVariable|$modifiers}

Php script

$smarty->assign('myVariable', "brumla brumla na drum drum drum");
$smarty->assign('modifiers', "truncate:30|trim");

Or I would like to apply modifiers in php - is there any method for parsing and applying modifiers in php?

Thanks for answers.


Solution

  • Each Smarty modifier is really PHP function called smarty_modifier_$name(). This function can be called as any other.

    So in that example code you'd just do:

    <?php
    $myVariable = "brumla brumla na drum drum drum";
    $myVariable = smarty_modifier_truncate($myVariable, 30);
    $myVariable = smarty_modifier_trim($myVariable);
    $smarty->assign('myVariable', $myVariable);
    

    Of course you can use call_user_func() to make it more dynamic.