سؤال

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);
}
هل كانت مفيدة؟

المحلول

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);
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top