ranlt / smarty-php

Automatically exported from code.google.com/p/smarty-php
0 stars 0 forks source link

php notice w/ foreachelse on non-empty array #208

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Enable php's E_NOTICE error reporting
2. Populate a template variable with an array containing >= 1 element
3. Utililze the {foreach}{foreachelse}{/foreach} construct

Issue:
If the array is empty, then no errors
If the array is non-empty, and no {foreachelse}, then no errors
If the array is non-empty, and {foreachelse}, then error

What version of the product are you using? On what operating system?
PHP 5.5.15 (IIS/FastCGI)
Smarty 3.1.19
Windows 8

Please provide any additional information below.

I "fixed" this on my local copy by modifying 
smarty_internal_compile_foreach.php's line # 195 from:

return "<?php }\nif (!\$_smarty_tpl->tpl_vars[$item]->_loop) {\n?>";

to:

return "<?php }\nif (isset(\$_smarty_tpl->tpl_vars[$item]->_loop) && 
(!\$_smarty_tpl->tpl_vars[$item]->_loop)) {\n?>";

There likely is a better solution however that just ensures that the ->_loop 
property exists even on a non-empty array when hitting the foreachelse

Original issue reported on code.google.com by Scootin...@gmail.com on 16 Oct 2014 at 3:31

Attachments:

GoogleCodeExporter commented 9 years ago
{foreach} does always initialize $_smarty_tpl->tpl_vars[$item]->_loop.
The only case I can think of is when you assign a new value to the loop 
variable within the foreach loop. 

Did you do that?

Original comment by Uwe.Tews@googlemail.com on 16 Oct 2014 at 6:15

GoogleCodeExporter commented 9 years ago
... I am - that very well could be the issue then

Original comment by Scootin...@gmail.com on 16 Oct 2014 at 6:27

GoogleCodeExporter commented 9 years ago
Yes that is the problem. 
Assigning a new value within the loop would break also the usage of other 
special properties like  $item@iteration   etc.

So you patch will not resolve the general problem.

So I would update the documentation that the $item variable must not be 
overwritten within the loop as there is no easy fix in the code. 

Original comment by Uwe.Tews@googlemail.com on 16 Oct 2014 at 6:39