|
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 |
Yaro Smarty n00b
Joined: 09 Feb 2005 Posts: 1 Location: Kyiv, Ukraine
|
Posted: Tue Jun 20, 2006 9:50 am Post subject: empty() func in {if} causes errors with $default_modifiers |
|
|
If you use empty() func in your templates conditional expressions it will cause an error when using $smarty->default_modifiers = array(some_modifiers);
After investigating the subject I have found out that the root of the problem lies in the fact that on processing templates through default_modifiers we get expressions like:
Code: | if empty(CONST) { do something } |
which in template looks like:
Code: | {if empty($somevar)} do something {/if} |
And per PHP Manual ( http://www.php.net/empty ):
Quote: | ...
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).
... |
I was using empty() all over the place and the application is very big and difficult (2 years of development) with a lot of templates and now I need to use $default_modifiers but stucked with that issue ...
Any hints/ideas? Any easy fix for this with release of Smarty 2.6.15 maybe? |
|
Back to top |
|
El Hombre Gris Smarty Rookie
Joined: 21 Apr 2006 Posts: 23
|
Posted: Thu Jun 22, 2006 1:20 pm Post subject: |
|
|
This is not a Smarty bug but a misuse of empty() which is not a function but a special keyword of PHP, which means that empty accepts only variables. Using a modifier means that the variable will be passed to a function therefore empty() can't be used with modifiers, even when these are implicit (default modifiers).
If you wish to check the value of the variable after being passed through modifiers, you can omit empty and let PHP cast the value to boolean, using a logic not operator to check for emptyness:
Code: | {if !$somevar} do something {/if} |
If you want to check the value of the variable before applying default modifiers:
Code: | {if !$somevar|smarty:nodefaults} do something {/if} |
Or:
Code: | {if empty($somevar|smarty:nodefaults)} do something {/if} |
This last example is valid since smarty won't apply any function to the variable, and empty will receive the variable itself. |
|
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
|