Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Reversing Smarty's array_map functionality

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks
View previous topic :: View next topic  
Author Message
mankyd
Smarty Regular


Joined: 04 May 2006
Posts: 92
Location: Boston MA

PostPosted: Fri May 16, 2008 10:32 pm    Post subject: Reversing Smarty's array_map functionality Reply with quote

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
View user's profile Send private message
mankyd
Smarty Regular


Joined: 04 May 2006
Posts: 92
Location: Boston MA

PostPosted: Sun Oct 26, 2008 12:48 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Tips and Tricks All times are GMT
Page 1 of 1

 
Jump to:  
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
Protected by Anti-Spam ACP