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

non array values for from in 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
Surt
Smarty Rookie


Joined: 25 May 2007
Posts: 8

PostPosted: Mon May 28, 2007 11:00 am    Post subject: non array values for from in foreach Reply with quote

Hi could you take in consideration:

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


as discussed here:
http://www.phpinsider.com/smarty-forum/viewtopic.php?p=42336#42336
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 11:43 am    Post subject: Reply with quote

The problem line appears to be in Smarty_Compiler.class.php:1203 in function _compile_foreach_start - where the code:

Code:
    if (isset($name)) {
            $foreach_props = "\$this->_foreach[$name]";
            $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
            $output .= "if ({$foreach_props}['total'] > 0):\n";
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
            $output .= "        {$foreach_props}['iteration']++;\n";
        } else {
            $output .= "if (count(\$_from)):\n";
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
        }


should be:

Code:
        if (isset($name)) {
            $foreach_props = "\$this->_foreach[$name]";
            $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
            $output .= "if ({$foreach_props}['total'] > 0):\n";
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
            $output .= "        {$foreach_props}['iteration']++;\n";
        } else {
            $output .= "if (count(\$_from)-1):\n"; // Problem line
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
        }


There are 2 options to fixing it:

1) if (count(\$_from) && \$_from===array()):
2) if (count($_from)-1):

(2) follows more of the pattern defined with other uses of count() in the compiler class - not sure how compatible (1) is with earlier versions of PHP.

Not sure, however, what (2) this would break.

(this should really be in the bugs forum).
_________________
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
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Mon May 28, 2007 4:06 pm    Post subject: Reply with quote

No, foreach does not work only on arrays. I can't recall the version number (maybe 2.6.8 -- you can check the changelon) but we relaxed the array only restriction to permit PHP5 iterable objects.
Back to top
View user's profile Send private message
Surt
Smarty Rookie


Joined: 25 May 2007
Posts: 8

PostPosted: Tue May 29, 2007 9:03 am    Post subject: Reply with quote

Ummm Dasher solution doesn't resolve my problems, when there is 1 index in the array returns 0 iterations.

i resolve it (but is just for false, 0 values...) this way:

Code:

        $output = '<?php ';
      $output .=  "\$_from = $from; if(!\$_from) \$_from = array();"; // if $_from is false
        $output .= "if (!is_array(\$_from) && !is_object(\$_from)) { settype(\$_from, 'array'); }";
        if (isset($name)) {
            $foreach_props = "\$this->_foreach[$name]";
            $output .= "{$foreach_props} = array('total' => count(\$_from), 'iteration' => 0);\n";
            $output .= "if ({$foreach_props}['total'] > 0):\n";
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
            $output .= "        {$foreach_props}['iteration']++;\n";
        } else {
            $output .= "if (count(\$_from)):\n";
            $output .= "    foreach (\$_from as $key_part\$this->_tpl_vars['$item']):\n";
        }
        $output .= '?>';

        return $output;


second line, if from is false from = array();
Back to top
View user's profile Send private message
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