|
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 |
andyl56 Smarty n00b
Joined: 26 Feb 2005 Posts: 3
|
Posted: Sat Feb 26, 2005 8:57 pm Post subject: [Complete] Allow if in_array |
|
|
I was in need of an if in_array tag for the templates for a program I am making so I added it. It would be nice if this got added to smarty.
Use:
{if $val is [not] in_array $array}Blah{/if}
Edit File: Smarty_Compiler.class.php
(Addition to function _parse_is_expr in the switch)
Line No# + What to add
1441 case 'in_array':
1442 $expr_arg = $tokens[$expr_end++];
1443 $expr = "is_array(".$this->_parse_var_props($expr_arg).") && in_array($is_arg, ".$this->_parse_var_props($expr_arg).")";
1444 break;
1445 _________________ - Andrew (andyl56) |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Sat Feb 26, 2005 11:25 pm Post subject: |
|
|
No need to modify the core. Smarty already allows you to use PHP functions as modifiers (so long as security is turned off). If security is on, you obtain the same by registering a wrapper function (in this case a modifier). So:
Code: | {"foo"|in_array:$bar} |
is already valid in Smarty.
So is:
Code: | {if "foo"|in_array:$bar}got 'foo' in '$bar'{/if} |
|
|
Back to top |
|
andyl56 Smarty n00b
Joined: 26 Feb 2005 Posts: 3
|
Posted: Sun Feb 27, 2005 12:02 am Post subject: |
|
|
Thanks... could this at least be included of the "if,elseif,else" part of the manual? I think it would help other people to know it's there. _________________ - Andrew (andyl56) |
|
Back to top |
|
andyl56 Smarty n00b
Joined: 26 Feb 2005 Posts: 3
|
Posted: Sun Feb 27, 2005 1:36 am Post subject: Bug Fix |
|
|
There is a problem with this way of doing things. When the array is not set yet (for examply, if the array is in a POST query on the first time the form page is loaded), then you'll get some PHP error messages complaining about it being undefined.
Fix: in Smarty_Complier.class.php around line 1924 (towards the end of the _parse_modifiers function, the line reads:
Quote: | $output = "((is_array(\$_tmp=$output)) ? \$this->_run_mod_handler('$_modifier_name', true, \$_tmp$_modifier_args) : " . $this->_compile_plugin_call('modifier', $_modifier_name) . "(\$_tmp$_modifier_args))"; |
change it to:
Quote: | $output = "((is_array(\$_tmp=$output)) ? \$this->_run_mod_handler('$_modifier_name', true, \$_tmp$_modifier_args) : @" . $this->_compile_plugin_call('modifier', $_modifier_name) . "(\$_tmp$_modifier_args))"; |
(Added @ before the function is_array is called making php ignore any errors) _________________ - Andrew (andyl56) |
|
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
|