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

smarty 2.x - detect if array modifier was called with @

 
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 -> General
View previous topic :: View next topic  
Author Message
carpii
Smarty Rookie


Joined: 06 Sep 2008
Posts: 18

PostPosted: Wed Jun 12, 2019 12:22 pm    Post subject: smarty 2.x - detect if array modifier was called with @ Reply with quote

I have a simple modifier

Code:

public static function ArraySizeOf($array)
   {
      if (isset($array) && is_array($array))
      {
         return count($array);
      }
      else
         return 0;
   }


I register this with smarty as a modifier.

As a failsafe, I want to throw an error if the modifier is not called with @

ie {if $myarray|arraysizeof > 0} would throw an error
{if $myarray|@arraysizeof > 0} would not throw an error

Is this possible? I can't think how to detect it inside the modifier function, even though I can see the parameters are passed slightly differently by Smarty

Thanks


PS: I understand I can already use {if sizeof($array) > 0} or {$array|@count} but this is not quite what I want
Back to top
View user's profile Send private message
bsmither
Smarty Elite


Joined: 20 Dec 2011
Posts: 322
Location: West Coast

PostPosted: Wed Jun 12, 2019 7:06 pm    Post subject: Reply with quote

Having made a very brief scan of the Smarty_Compiler class (version 2.6.31), function _parse_modifiers(), line 1923, I see that the '@' prefix is removed from the $_modifier_name and a $_map_array flag is set to false (operate on the array as an entity).

I believe a solution that pre-filters the template with a regex for the name of all the targeted modifiers. Such as:
Code:
'~\$\w+\|\@arraysizeof~'
that, if matched, has the '@' and you can set a global flag true|false that your modifier function can test for.
Back to top
View user's profile Send private message
carpii
Smarty Rookie


Joined: 06 Sep 2008
Posts: 18

PostPosted: Fri Jun 14, 2019 4:33 am    Post subject: Reply with quote

Thanks, very helpful.

I ended up just adding a specific check in the compiler class..

Code:

            if (substr($_modifier_name, 0, 1) == '@') {
                $_map_array = false;
                $_modifier_name = substr($_modifier_name, 1);
            } else {
                $_map_array = true;

                if ($_modifier_name == "arraysizeof") {
                    throw new Exception("cannot use arraysizeof without @array operator");
                }
            }


Not at all generic, but it's only for this specific modifier while I migrate a codebase to PHP 7. I will then remove it and replace it with a git hook

Thanks
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 -> General 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