-
Notifications
You must be signed in to change notification settings - Fork 93
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
f:variable not working correctly inside f:for VH #846
Comments
Because And of course, variable access prefers the local scope. |
Yes, but in my opinion, this behaviour is wrong. In theory, you now have two variables with the same name inside the loop, but you can only access one. This does not make sense. If you pass the "as" variable down using the render VH, it works as intended: <f:for each="{myNumbers}" as="number">
<f:render section="MySection" arguments="{_all}" />
</f:for>
<f:section name="MySection">
<f:if condition="{number} == 2">
<!-- Now it works -->
{f:variable(name: 'number', value: '99')}
</f:if>
<div>
{number}
</div>
</f:section> The behaviour is weird and inconsistent. You should be able to update the "as" variable inside the loop, without affecting the global scope. |
Hi @timothebot! The change to differentiate between global and local variables was made to make Fluid more predictable in other areas. However, I agree that the behavior you describe here is a problem. One possible solution could be to "convert" an existing local variable to a global variable once it's set explicitly via VariableViewHelper. I don't know if this will have other side effects, but do you think that this could work for your case? In practice this would mean that $globalVariables->add('myForVariable', 123);
$localVariables->remove('myForVariable'); |
@timothebot Could you check if the attached PR fixes your issue? |
@s2b It does fix the issue and it's definitely an improvement, but I still think it's weird that loops behave differently in Fluid than in PHP. |
What exactly do you mean? Loops also behaved differently before these changes: The "local" variables were reset after the loop and could not be used afterwards. |
@s2b ah yes, sorry, you are right. In this case it's okay 👍 |
This commit changed the behaviour of the "as" variable. Previously, it was added to the global Template Variable Container, now it gets added to the Local Variable Container.
What I don't understand is why you can't update the as variable inside the loop anymore:
This used to work, but does not work anymore. It makes sense that the variable can't be accessed outside the loop, but why is it static inside it?
It would make sense that you can update it inside the loop.
The text was updated successfully, but these errors were encountered: