Open tophattom opened 7 years ago
The assign tag writes to the last scope in the context. Everything else in liquid writes (and reads) from the scopes from first through last.
def render(context)
val = @from.render(context)
context.scopes.last[@to] = val
context.resource_limits.assign_score += assign_score_of(val)
''.freeze
end
It feels like a bug, but it's been the behaviour of liquid since forever.
The behaviour likely exists to allow the assign method to work within a nested blocks.
{% for page in page %}
{% if page.awesome %}
{% assign awesome_page = page %}
{% break %}
{% endif %}
{% endfor %}
{% if awesome_page %}
The page {{page.title}} is awesome.
{% endif %}
It's super-wonky, but changing the behavior would likely break existing templates.
The best solution would probably be to change the assign method to search through the scopes to find where the variable was assigned and modify it in that scope. It's weird that only variables set in the root scope can be reassigned.
In template:
my-snippet.liquid
Results in
foo
instead ofbar
.However, everything works as expected when including the snippet like this: