|
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 |
jaketseng Smarty Rookie
Joined: 11 Dec 2010 Posts: 6
|
Posted: Fri Dec 17, 2010 8:57 am Post subject: add if to foreach |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Dec 17, 2010 3:42 pm Post subject: |
|
|
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 |
|
jaketseng Smarty Rookie
Joined: 11 Dec 2010 Posts: 6
|
Posted: Sat Dec 18, 2010 4:09 am Post subject: |
|
|
“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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Sat Dec 18, 2010 5:04 am Post subject: |
|
|
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 |
|
jaketseng Smarty Rookie
Joined: 11 Dec 2010 Posts: 6
|
Posted: Mon Dec 20, 2010 1:51 am Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Mon Dec 20, 2010 3:42 am Post subject: |
|
|
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 |
|
|
|
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
|