Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

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

 
Post new topic   Reply to topic    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 Pro


Joined: 20 Dec 2011
Posts: 242
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:   
Post new topic   Reply to topic    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