Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
empty() func in {if} causes errors with $default_modifiers

 
Post new topic   Reply to topic    Smarty Forum Index -> Bugs
View previous topic :: View next topic  
Author Message
Yaro
Smarty n00b


Joined: 09 Feb 2005
Posts: 1
Location: Kyiv, Ukraine

PostPosted: Tue Jun 20, 2006 9:50 am    Post subject: empty() func in {if} causes errors with $default_modifiers Reply with quote

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 ... Sad

Any hints/ideas? Any easy fix for this with release of Smarty 2.6.15 maybe? Wink
Back to top
View user's profile Send private message Yahoo Messenger
El Hombre Gris
Smarty Rookie


Joined: 21 Apr 2006
Posts: 23

PostPosted: Thu Jun 22, 2006 1:20 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Bugs 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