liquid icon indicating copy to clipboard operation
liquid copied to clipboard

Hash registers leaking into subcontexts as static registers

Open dylanahsmith opened this issue 3 years ago • 0 comments

Here is an example that shows the problem:

require 'liquid'

template_code = <<~LIQUID
  {% render 'snippet' %}
LIQUID

module FakeFS
  def self.read_template_file(template_path)
    case template_path
    when 'snippet'
      <<~LIQUID
        In snippet:
        {% cycle "colors": 'red', 'green', 'blue' %}
        {% render 'subsnippet' %}
      LIQUID
    when 'subsnippet'
      <<~LIQUID
        In subsnippet:
        {% cycle "colors": 'red', 'green', 'blue' %}
      LIQUID
    end
  end
end

Liquid::Template.file_system = FakeFS

puts Liquid::Template.parse(<<~LIQUID).render.strip
  template 1:
  {% render 'snippet' %}
LIQUID
puts
puts Liquid::Template.parse(<<~LIQUID).render.strip
  template 2:
  {% cycle "colors": 'red', 'green', 'blue' %}
  {% render 'snippet' %}
LIQUID

which outputs

template 1:
In snippet:
red
In subsnippet:
red

template 2:
red
In snippet:
green
In subsnippet:
blue

where for template 1 you can see that the :cycle register isn't shared for the nested render, as expected, but this isn't the case for template 2. For template 2, the :cycle register gets set by the cycle tag in the template, then the snippets are inheriting that register as a static register.

The output I would expect would be

template 1:
In snippet:
red
In subsnippet:
red

template 2:
red
In snippet:
red
In subsnippet:
red

dylanahsmith avatar Apr 05 '22 14:04 dylanahsmith