|
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 |
carpii Smarty Rookie
Joined: 06 Sep 2008 Posts: 18
|
Posted: Wed Jun 12, 2019 12:22 pm Post subject: smarty 2.x - detect if array modifier was called with @ |
|
|
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 |
|
bsmither Smarty Elite
Joined: 20 Dec 2011 Posts: 322 Location: West Coast
|
Posted: Wed Jun 12, 2019 7:06 pm Post subject: |
|
|
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 |
|
carpii Smarty Rookie
Joined: 06 Sep 2008 Posts: 18
|
Posted: Fri Jun 14, 2019 4:33 am Post subject: |
|
|
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 |
|
|
|
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
|