View previous topic :: View next topic |
Author |
Message |
blaz Smarty n00b
Joined: 13 May 2003 Posts: 2
|
Posted: Tue May 13, 2003 11:28 am Post subject: Is array empty? |
|
|
How to use a smarty IF statement depending on if an array is empty?
I have an array called $errors. This doesn't seem to work...
Code: |
{if $errors ne ''}
...some code here...
{/if}
|
Thanx,
Blaz |
|
Back to top |
|
AZTEK Smarty Pro
Joined: 16 Apr 2003 Posts: 235 Location: Purdue University
|
Posted: Tue May 13, 2003 2:47 pm Post subject: |
|
|
Humm good point I just tried and I dont know how you would do that without changing Smarty _________________ "Imagine a school with children that can read and write, but with teachers who cannot, and you have a metaphor of the Information Age in which we live." -Peter Cochrane |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue May 13, 2003 3:59 pm Post subject: |
|
|
hmm
{if $array ne ''} ... tests if the array is *not* empty.
possible tests for empty array:
{if $array eq ''}
{if !$array}
{if not $array}
these all are not type-specific (they also match for empty strings, empty objects, false, null, 0 and 0.0). but types are something for php, not for smarty
edit: correction, {if $array eq ''} does not work, but the other two do.
Last edited by messju on Tue May 13, 2003 4:16 pm; edited 1 time in total |
|
Back to top |
|
sweatje Smarty Regular
Joined: 17 Apr 2003 Posts: 70 Location: Bettendorf, Iowa, USA
|
Posted: Tue May 13, 2003 4:04 pm Post subject: |
|
|
I use
Code: | {if $errors|@count gt 0}
... some stuff here ...
{/if}
|
HTH _________________ Jason
jsweat_php AT yahoo DOT com |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Tue May 13, 2003 5:04 pm Post subject: |
|
|
I tended to use sweatje's method (and sometimes the ne "" method--I think that worked in previous versions??) but I decided to see just what the difference was performance wise (since modifiers don't come for free).
Using my handy-dandy timeblock plugin and the following template code:
{timeblock}
{if $array|@count == 0}EMPTY{/if}
{/timeblock}
{timeblock}
{if !$array}EMPTY{/if}
{/timeblock}
{timeblock}
{if not $array}EMPTY{/if}
{/timeblock}
I ran tests both with empty arrays and arrays populated with 1000 elements. Cutting to the chase:
$array|@count == 0 is about 10 times slower than the other two methods.
!$array and not $array run about neck-and-neck.
For fairness, you must run each timeblock separately. Otherwise, PHP seems to cache (?) the lookup result on the array causing subsequent checks to be faster.
The moral is:
If you get advice from more than one person, pay particular attention to what messju is saying!! |
|
Back to top |
|
messju Administrator
Joined: 16 Apr 2003 Posts: 3336 Location: Oldenburg, Germany
|
Posted: Tue May 13, 2003 10:44 pm Post subject: |
|
|
ah, no boots. i know of bugs i committed to smarty-cvs. of course i fixed 'em asap when i noticed, but my choice wasn't the best before then.
all in all: "good" is a vector and "fast" is only a component.
but your post makes me ponder, if modifier-calls to php-functions should be resolved at compile-time instead of run-time. this would bring the 'count' into the compiled template literally. i think this will have an impact of native-php modifier's execution time.
just some thoughts, that are totally off-topic here. |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 14, 2003 12:25 am Post subject: |
|
|
messju--I didn't mean to put undue pressure on you--I was just having fun there Apologies to sweatje as well: as I said, I made the same choice as you did -- it seems natural to test strings against "" and arrays against @count. I was even using $var|count == 0 to test strings for consistency with $array|@count == 0.
Quote: | all in all: "good" is a vector and "fast" is only a component. |
absolutely! In this case, given three otherwise equal choices, I'll take the fastest.
On the topic of your off-topic comments, I would love to see compiled modifiers -- recall my query to add compile-time modifier plugins. I think it might also lead to ubiquitous support for modifiers whereas right now, they don't operate on builtins or compiler functions.
One thing I like about benchmarking is that it forces comparisons and therefore brings up things I might not otherwise consider. |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Wed May 14, 2003 4:32 pm Post subject: |
|
|
yeah... boots... but you know, often a |@count can't be avoided...
I _always_ initialize arrays ($foo = array();) before I populate and assign them, so I can't do a !$array in an IF... ;/ |
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Wed May 14, 2003 4:55 pm Post subject: |
|
|
Wom.bat:
Quote: | yeah... boots... but you know, often a |@count can't be avoided...
I _always_ initialize arrays ($foo = array() before I populate and assign them, so I can't do a !$array in an IF... ;/ |
did you try testing that?
---
$smarty->assign('b', array());
---
{if !$a}Empty{/if}
{if !$b}Empty{/if}
---
EmptyEmpty
So, initialized (but empty) arrays and uninitialed vars both eval to true for !$var. |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Thu May 15, 2003 2:17 pm Post subject: |
|
|
w000t
well... in php, it would be false
sorry for that mistake
|
|
Back to top |
|
boots Administrator
Joined: 16 Apr 2003 Posts: 5611 Location: Toronto, Canada
|
Posted: Thu May 15, 2003 5:27 pm Post subject: |
|
|
Wom.Bat:
Yeah, I see why you thought that and my feeling is that the behaviour that Smarty is exhibiting is not by policy but instead an artifact of the implementation. I too would have expected your result (ie. consistent with PHP) but it was in timing the different checking styles that I noticed this behaviour. So I wasn't trying to "correct" you--only showing what the actual behaviour appears to be
Last edited by boots on Thu May 15, 2003 7:50 pm; edited 1 time in total |
|
Back to top |
|
Wom.bat Smarty Pro
Joined: 24 Apr 2003 Posts: 107 Location: Munich, Germany
|
Posted: Thu May 15, 2003 7:21 pm Post subject: |
|
|
hehe not a problem, thank you very much
P.S: have you tested the math patch yet? |
|
Back to top |
|
|