|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
mankyd Smarty Regular
Joined: 04 May 2006 Posts: 92 Location: Boston MA
|
Posted: Fri May 16, 2008 10:32 pm Post subject: Reversing Smarty's array_map functionality |
|
|
When you apply a modifier to a variable in Smarty it will, by default, test to see if it is an array. If it is, it runs array_map() on the array, applying the function to each element of the array rather than passing the unaltered array to the modifier. This functionality can be undone by prepending an @ to modifier. I.E.: {$variable|@modifier}
On our site, we never needed this functionality. As a matter of fact, we never wanted this functionality and the additional additional is_array check with each of our modifiers was wasted cpu cycles. I have written a rather simplistic smarty-prefilter that reverses the behavior of the @ symbol:
Code: | function smarty_prefilter_reverse_array_map($source, &$smarty) {
$ldq = preg_quote($smarty->left_delimiter, '~');
$rdq = preg_quote($smarty->right_delimiter, '~');
return preg_replace_callback("~{$ldq}\s*(.*?)\s*{$rdq}~s", create_function('$m', 'return strtr($m[0],array("||"=>"||","|"=>"|@","|@"=>"|"));'), $source);
} |
I don't know that it's perfect, but it works for our purposes. |
|
Back to top |
|
mankyd Smarty Regular
Joined: 04 May 2006 Posts: 92 Location: Boston MA
|
Posted: Sun Oct 26, 2008 12:48 am Post subject: |
|
|
I just read the news that Smarty 3 is under development. Congrats guys! (there is a certain level of irony for me here, as I am working on a smarty-like templating engine for another language.)
I just wanted to give this thread a bump as a feature I would love to see built into Smarty 3. A simple boolean value to reverse the array_map behavior would be fantastic. |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|