Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{foreachelse} not showing with Iterator 0 elements #562

Closed
tom-2015 opened this issue Jan 10, 2020 · 5 comments
Closed

{foreachelse} not showing with Iterator 0 elements #562

tom-2015 opened this issue Jan 10, 2020 · 5 comments

Comments

@tom-2015
Copy link

tom-2015 commented Jan 10, 2020

Recently updated to PHP7.3 and Smarty 3.1.34 and noticed that {foreachelse} is not executed when the data count is 0.

I think this is because $needTotal parameter in smarty_internal_runtime_foreach.php:44 is false and $total will always be 1 so function will not return null if total items in array/Iterator is 0.

smarty_internal_runtime_foreach.php

if (!isset($total)) {
$total = empty($from) ? 0 : ($needTotal ? count($from) : 1);
}
......
if ($total === 0) {
$from = null; //should return 0 if {foreachelse} is used
} else {

To fix this smarty_internal_compile_foreach.php:190 should check if total is needed and/or foreachelse tag is present. I have added "|| true" so it will always do a count() on the supplied array. Not best fix but I don't know how to check if a {foreachelse} tag is present.

smarty_internal_compile_foreach.php:190
$needTotal = isset($itemAttr[ 'total' ]) || true; //check if {foreachelse} tag is present instead of || true ...

@caugner
Copy link
Contributor

caugner commented Jan 13, 2020

What Smarty version did you use before?

@tom-2015
Copy link
Author

It was working with 3.1.14

@caugner
Copy link
Contributor

caugner commented Jan 15, 2020

Could you try with 3.1.30 and 3.1.31 please? If it works in 3.1.30, but not in 3.1.31, it's probably due to this change:

smarty/change_log.txt

Lines 302 to 303 in b2ecd98

09.09.2016
- bugfix/optimization {foreach} did not execute the {foreachelse} when iterating empty objects https://github.com/smarty-php/smarty/pull/287

@tom-2015
Copy link
Author

I've tested and it works correctly with 3.1.31, 3.1.32
does not with: 3.1.30, 3.1.33, 3.1.34

Problem only occurs when using objects like class that implements iterator.

@wisskid
Copy link
Contributor

wisskid commented Jan 27, 2020

this was fixed in #566

@wisskid wisskid closed this as completed Jan 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants