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

foreach and false $from values

 
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 -> General
View previous topic :: View next topic  
Author Message
Surt
Smarty Rookie


Joined: 25 May 2007
Posts: 8

PostPosted: Fri May 25, 2007 2:02 pm    Post subject: foreach and false $from values Reply with quote

Hi,

i recently updated my smarty version.
In my project i am returning FALSE when no values from database are returned from a query. Well, at last until i know, my older version of smarty jumped to the {foreachelse} when the $from value was false.

example:

if i have the variable $results = false

Code:

     {foreach from=$results item=actual_result}
                  <p>normal iteration</p>
    {foreachelse}
                   <p>default for false instead array</p>
    {/foreach}                 



It is possible to do it this way now? My actual version .18 gives me 1 iteration with empty values.
I was triying to look at the forums but no luck.
Back to top
View user's profile Send private message
Dasher
Smarty Pro


Joined: 16 May 2007
Posts: 124
Location: Italy

PostPosted: Fri May 25, 2007 2:11 pm    Post subject: Reply with quote

I'm a little confused - you mean this:

Code:
{if ! $results eq false}
  {foreach from=$results item=actual_result}
      <p>normal iteration</p>
  {foreachelse}
         <p>default for false instead array - just return an empty array and you'll hit this part</p>
  {/foreach}
{else}
         <p>default for false instead array</p>
{/endif}

_________________
General Blog: http://dasher.wordpress.com
VoIP & SIP: http://blog.sipx.ws
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Surt
Smarty Rookie


Joined: 25 May 2007
Posts: 8

PostPosted: Mon May 28, 2007 7:27 am    Post subject: Reply with quote

No, sorry

my variable $results = false

now i have this one:
Code:

{if $results} 
       {foreach from=$results......}
               ..........
        {/foreach}
{/else}
        <p>There is no results to show</p>
{/if}


but with the other version of smarty (dont know the v.number) i could make this one:

Code:
 {foreach from=$results....}
            ........
      {foreachelse}
         <p>There is no results to show</p>
      {/foreach}


Do you know what i mean? Now with the .18 (dont know other versions) i can set $results to false. The foreach returns me always one iteration without data.
Back to top
View user's profile Send private message
Dasher
Smarty Pro


Joined: 16 May 2007
Posts: 124
Location: Italy

PostPosted: Mon May 28, 2007 7:33 am    Post subject: Reply with quote

ah ok.

If you need $results to to be to false then what you have shown will work.

Code:
{if $results} 
       {foreach from=$results......}
               ..........
        {/foreach}
{else}
        <p>There is no results to show</p>
{/if}


But if you're going to change something - unless you need $results to be false for some other reason - you may as well have results returned as an empty array.

Then your templates would work as expected with the {foreachelse} and you end up with slightly simpler syntax in your templates.
_________________
General Blog: http://dasher.wordpress.com
VoIP & SIP: http://blog.sipx.ws
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Surt
Smarty Rookie


Joined: 25 May 2007
Posts: 8

PostPosted: Mon May 28, 2007 7:51 am    Post subject: Reply with quote

ohh Sad

there is no way to change the core to detect false $results and maintain the simple syntax?

I am working with a "execute query" function that return false in case of null results. It is more easy for me to control the returned querys to database at the classes who feed with it.

In a abstract definition of the core of my framework i prefer to have arrays of values or ,(in case of null values), false. Donīt you think that it's better this way?
I know, mysql_query returns array()... but mysql_query is just a tool for my "execute query" function.
Back to top
View user's profile Send private message
Dasher
Smarty Pro


Joined: 16 May 2007
Posts: 124
Location: Italy

PostPosted: Mon May 28, 2007 9:00 am    Post subject: Reply with quote

tbh - I always return an empty array rather than false - so I wouldn't know if the functionality had changed in .18 from previous versions.

Personally I prefer to use empty arrays - but it's a matter of personal choice (and making your smarty templates a little simpler).

I've only has one coffee this morning and the quickest way to solve the problem that comes to mind is using a modifier (I'm sure somebody will come up with a better solution).

This would allow the syntax like:

Code:
{foreach from=$results|valid_results item=a key=b}
  We have valid array<br/>
{foreachelse}
  We had something that wasn't a valid array<br/>
{/foreach}


where the modifier would be:

modifier.valid_results.php
Code:
function smarty_modifier_valid_results($testValue) {
   
   if (is_array($testValue)) {
      return $testValue;
   }else{
      return array();
   }
}

_________________
General Blog: http://dasher.wordpress.com
VoIP & SIP: http://blog.sipx.ws
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
Surt
Smarty Rookie


Joined: 25 May 2007
Posts: 8

PostPosted: Mon May 28, 2007 9:07 am    Post subject: Reply with quote

hahaha i want one of your coffes! that was a good idea!

I will try to use the modifier, thanks a lot for your replys.

Anyway, will be good that smarty developers take the posibility of non empty arrays in from values, especially false or 0 values.
Back to top
View user's profile Send private message
Dasher
Smarty Pro


Joined: 16 May 2007
Posts: 124
Location: Italy

PostPosted: Mon May 28, 2007 9:27 am    Post subject: Reply with quote

As {foreach} should only work on arrays - I would guess that it would be a good idea for anything other than an array to trigger the {foreachelse}.
_________________
General Blog: http://dasher.wordpress.com
VoIP & SIP: http://blog.sipx.ws
Back to top
View user's profile Send private message Visit poster's website MSN Messenger
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 -> General 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