From 740241a9970c0238ce77bd4c651c5afdc98692ba Mon Sep 17 00:00:00 2001 From: Mike Angell Date: Tue, 17 Sep 2019 01:59:55 +1000 Subject: [PATCH] Enable Instance Assigns to survive many renders --- lib/liquid/template.rb | 5 +++-- test/integration/template_test.rb | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/liquid/template.rb b/lib/liquid/template.rb index 286dbd23..58fb94f8 100644 --- a/lib/liquid/template.rb +++ b/lib/liquid/template.rb @@ -143,11 +143,12 @@ module Liquid end def instance_assigns - @instance_assigns ||= {} + @instance_assigns ||= [] end def new_outer_scope - @instance_assigns = {} + @instance_assigns.unshift(last = {}) + last end def errors diff --git a/test/integration/template_test.rb b/test/integration/template_test.rb index e2b54c79..26c640b2 100644 --- a/test/integration/template_test.rb +++ b/test/integration/template_test.rb @@ -42,6 +42,18 @@ class TemplateTest < Minitest::Test assert_equal 'from instance assigns', t.parse("{{ foo }}").render! end + def test_instance_assigns_persist_on_same_template_object_between_many_parses + t = Template.new + assert_equal 'from instance assigns', t.parse("{% assign foo = 'from instance assigns' %}{{ foo }}").render! + assert_equal 'from instance assigns', t.parse("{{ foo }}").render! + assert_equal 'from instance assigns', t.parse("{{ foo }}").render! + assert_equal 'from instance assigns', t.parse("{{ foo }}").render! + assert_equal 'from instance assigns second', t.parse("{% assign foo = 'from instance assigns second' %}{{ foo }}").render! + assert_equal 'from instance assigns second', t.parse("{{ foo }}").render! + assert_equal 'from instance assigns second', t.parse("{{ foo }}").render! + assert_equal 'from instance assigns second', t.parse("{{ foo }}").render! + end + def test_warnings_is_not_exponential_time str = "false" 100.times do @@ -58,6 +70,14 @@ class TemplateTest < Minitest::Test assert_equal 'foofoo', t.render! end + def test_instance_assigns_persist_on_same_template_parsing_between_many_renders + t = Template.new.parse("{{ foo }}{% assign foo = 'foo' %}{{ foo }}") + assert_equal 'foo', t.render! + assert_equal 'foofoo', t.render! + assert_equal 'foofoo', t.render! + assert_equal 'foofoo', t.render! + end + def test_custom_assigns_do_not_persist_on_same_template t = Template.new assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')