Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

add if to foreach

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests
View previous topic :: View next topic  
Author Message
jaketseng
Smarty Rookie


Joined: 11 Dec 2010
Posts: 6

PostPosted: Fri Dec 17, 2010 8:57 am    Post subject: add if to foreach Reply with quote

hi,

some of the PHP template engine had a feature: if the var is empty, the "foreach" would not to run once.

smarty now is check the "foreach" var is "isset", not is "empty", so we must write "if" before "foreach" to check whether the var is empty.

in many condition, we "assign" the var, but the var might be FALSE or NULL, Smarty could not realize that and run once for null.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Dec 17, 2010 3:42 pm    Post subject: Reply with quote

Smarty 2 and 3 will go inside the loop once for 0 or false or empty string, but not for empty array() or null. behavior is not identical to php empty()
Back to top
View user's profile Send private message Visit poster's website
jaketseng
Smarty Rookie


Joined: 11 Dec 2010
Posts: 6

PostPosted: Sat Dec 18, 2010 4:09 am    Post subject: Reply with quote

“go inside the loop once for 0 or false or empty string” that is make no
sense, and that is a empty loop.
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Sat Dec 18, 2010 5:04 am    Post subject: Reply with quote

It does make sense... if you try to loop over a non-array value it will be treated as a single-item array. So values such as strings, integers, booleans will be treated as a single-item array. Non-values (empty array and null) will not be looped. The integer value 0 and boolean value false are not treated as "empty" but rather array(0) and array(false).
Back to top
View user's profile Send private message Visit poster's website
jaketseng
Smarty Rookie


Joined: 11 Dec 2010
Posts: 6

PostPosted: Mon Dec 20, 2010 1:51 am    Post subject: Reply with quote

It is no use for the practice, not for language.
in the real situation, if you got a null value(empty string, 0, false, null etc.) from the database or some where, you will not loop once because there is nothing at all.
i think there is not the language SHOULD BE what, there is the practice NEED what!
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Mon Dec 20, 2010 3:42 am    Post subject: Reply with quote

issue understood. however that is how Smarty 2 worked without any complaints. So for template use, be sure you assign empty array() or null to get the desired action. Changing that behavior now may cause more problems for others.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Feature Requests 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