Question

How would I extend the $insert so that I can pass multiple parameters into the function e.g {"Sample text %s this more %s."|inject:$foo:$foo2}.

At the moment it only works with 1 param.

/**
 * Smarty inject modifier plugin
 *
 * Type:     modifier<br>
 * Name:     inject<br>
 * Purpose:  sprintf with a IF empty wrapper
 *
 */
function smarty_modifier_inject($string, $insert)
{
    if(!empty($insert))
        return sprintf($string, $insert);
}
Was it helpful?

Solution

you have to modify inject function to take arbitrary number of arguments, like this:

function inject(){
    $args = func_get_args();
    if(count($args) > 1){
        return call_user_func_array('sprintf', $args);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top