mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
4 Commits
inline-com
...
prevent-sc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10f7ed4b9c | ||
|
|
3bf8470a49 | ||
|
|
740241a997 | ||
|
|
29dfe2aea4 |
@@ -21,9 +21,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_registers = {}, static_environments = {})
|
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil, static_registers = {}, static_environments = {})
|
||||||
@environments = [environments]
|
@environments = environments.is_a?(Array) ? environments : [environments]
|
||||||
@environments.flatten!
|
|
||||||
|
|
||||||
@static_environments = [static_environments].flat_map(&:freeze).freeze
|
@static_environments = [static_environments].flat_map(&:freeze).freeze
|
||||||
@scopes = [(outer_scope || {})]
|
@scopes = [(outer_scope || {})]
|
||||||
@registers = registers
|
@registers = registers
|
||||||
@@ -33,16 +31,14 @@ module Liquid
|
|||||||
@strict_variables = false
|
@strict_variables = false
|
||||||
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
||||||
@base_scope_depth = 0
|
@base_scope_depth = 0
|
||||||
squash_instance_assigns_with_environments
|
@interrupts = []
|
||||||
|
@filters = []
|
||||||
|
@global_filter = nil
|
||||||
|
|
||||||
self.exception_renderer = Template.default_exception_renderer
|
self.exception_renderer = Template.default_exception_renderer
|
||||||
if rethrow_errors
|
if rethrow_errors
|
||||||
self.exception_renderer = ->(_e) { raise }
|
self.exception_renderer = ->(_e) { raise }
|
||||||
end
|
end
|
||||||
|
|
||||||
@interrupts = []
|
|
||||||
@filters = []
|
|
||||||
@global_filter = nil
|
|
||||||
end
|
end
|
||||||
# rubocop:enable Metrics/ParameterLists
|
# rubocop:enable Metrics/ParameterLists
|
||||||
|
|
||||||
@@ -245,16 +241,5 @@ module Liquid
|
|||||||
rescue Liquid::InternalError => exc
|
rescue Liquid::InternalError => exc
|
||||||
exc
|
exc
|
||||||
end
|
end
|
||||||
|
|
||||||
def squash_instance_assigns_with_environments
|
|
||||||
@scopes.last.each_key do |k|
|
|
||||||
@environments.each do |env|
|
|
||||||
if env.key?(k)
|
|
||||||
scopes.last[k] = lookup_and_evaluate(env, k)
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end # squash_instance_assigns_with_environments
|
|
||||||
end # Context
|
end # Context
|
||||||
end # Liquid
|
end # Liquid
|
||||||
|
|||||||
@@ -143,7 +143,12 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
def instance_assigns
|
def instance_assigns
|
||||||
@instance_assigns ||= {}
|
@instance_assigns ||= []
|
||||||
|
end
|
||||||
|
|
||||||
|
def new_outer_scope
|
||||||
|
@instance_assigns.unshift(last = {})
|
||||||
|
last
|
||||||
end
|
end
|
||||||
|
|
||||||
def errors
|
def errors
|
||||||
@@ -178,11 +183,11 @@ module Liquid
|
|||||||
c
|
c
|
||||||
when Liquid::Drop
|
when Liquid::Drop
|
||||||
drop = args.shift
|
drop = args.shift
|
||||||
drop.context = Context.new([drop, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
|
drop.context = Context.new([drop, assigns].concat(instance_assigns), new_outer_scope, registers, @rethrow_errors, @resource_limits)
|
||||||
when Hash
|
when Hash
|
||||||
Context.new([args.shift, assigns], instance_assigns, registers, @rethrow_errors, @resource_limits)
|
Context.new([args.shift, assigns].concat(instance_assigns), new_outer_scope, registers, @rethrow_errors, @resource_limits)
|
||||||
when nil
|
when nil
|
||||||
Context.new(assigns, instance_assigns, registers, @rethrow_errors, @resource_limits)
|
Context.new([assigns].concat(instance_assigns), new_outer_scope, registers, @rethrow_errors, @resource_limits)
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Expected Hash or Liquid::Context as parameter"
|
raise ArgumentError, "Expected Hash or Liquid::Context as parameter"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,6 +42,18 @@ class TemplateTest < Minitest::Test
|
|||||||
assert_equal 'from instance assigns', t.parse("{{ foo }}").render!
|
assert_equal 'from instance assigns', t.parse("{{ foo }}").render!
|
||||||
end
|
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
|
def test_warnings_is_not_exponential_time
|
||||||
str = "false"
|
str = "false"
|
||||||
100.times do
|
100.times do
|
||||||
@@ -58,6 +70,14 @@ class TemplateTest < Minitest::Test
|
|||||||
assert_equal 'foofoo', t.render!
|
assert_equal 'foofoo', t.render!
|
||||||
end
|
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
|
def test_custom_assigns_do_not_persist_on_same_template
|
||||||
t = Template.new
|
t = Template.new
|
||||||
assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')
|
assert_equal 'from custom assigns', t.parse("{{ foo }}").render!('foo' => 'from custom assigns')
|
||||||
|
|||||||
Reference in New Issue
Block a user