mirror of
https://github.com/kemko/liquid.git
synced 2026-01-01 15:55:40 +03:00
Compare commits
7 Commits
v5.0.0
...
simplify_f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b3c6a523b5 | ||
|
|
c34aba4d1a | ||
|
|
8933044cb5 | ||
|
|
ba0edc661d | ||
|
|
c06a325b3b | ||
|
|
6590815b00 | ||
|
|
d1deb89085 |
@@ -15,6 +15,8 @@ module Liquid
|
|||||||
attr_reader :scopes, :errors, :registers, :environments, :resource_limits, :static_registers, :static_environments
|
attr_reader :scopes, :errors, :registers, :environments, :resource_limits, :static_registers, :static_environments
|
||||||
attr_accessor :exception_renderer, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
attr_accessor :exception_renderer, :template_name, :partial, :global_filter, :strict_variables, :strict_filters
|
||||||
|
|
||||||
|
BLANK_SCOPE = {}
|
||||||
|
|
||||||
# rubocop:disable Metrics/ParameterLists
|
# rubocop:disable Metrics/ParameterLists
|
||||||
def self.build(environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_registers: {}, static_environments: {})
|
def self.build(environments: {}, outer_scope: {}, registers: {}, rethrow_errors: false, resource_limits: nil, static_registers: {}, static_environments: {})
|
||||||
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_registers, static_environments)
|
new(environments, outer_scope, registers, rethrow_errors, resource_limits, static_registers, static_environments)
|
||||||
@@ -191,15 +193,12 @@ module Liquid
|
|||||||
def find_variable(key, raise_on_not_found: true)
|
def find_variable(key, raise_on_not_found: true)
|
||||||
# This was changed from find() to find_index() because this is a very hot
|
# This was changed from find() to find_index() because this is a very hot
|
||||||
# path and find_index() is optimized in MRI to reduce object allocation
|
# path and find_index() is optimized in MRI to reduce object allocation
|
||||||
index = @scopes.find_index { |s| s.key?(key) }
|
scope = (index = @scopes.find_index { |s| s.key?(key) }) && @scopes[index]
|
||||||
|
scope ||= (index = @environments.find_index { |s| s.key?(key) || s.default_proc }) && @environments[index]
|
||||||
|
scope ||= (index = @static_environments.find_index { |s| s.key?(key) }) && @static_environments[index]
|
||||||
|
scope ||= BLANK_SCOPE
|
||||||
|
|
||||||
variable = if index
|
variable = lookup_and_evaluate(scope, key, raise_on_not_found: raise_on_not_found).to_liquid
|
||||||
lookup_and_evaluate(@scopes[index], key, raise_on_not_found: raise_on_not_found)
|
|
||||||
else
|
|
||||||
try_variable_find_in_environments(key, raise_on_not_found: raise_on_not_found)
|
|
||||||
end
|
|
||||||
|
|
||||||
variable = variable.to_liquid
|
|
||||||
variable.context = self if variable.respond_to?(:context=)
|
variable.context = self if variable.respond_to?(:context=)
|
||||||
|
|
||||||
variable
|
variable
|
||||||
@@ -227,22 +226,6 @@ module Liquid
|
|||||||
|
|
||||||
attr_reader :base_scope_depth
|
attr_reader :base_scope_depth
|
||||||
|
|
||||||
def try_variable_find_in_environments(key, raise_on_not_found:)
|
|
||||||
@environments.each do |environment|
|
|
||||||
found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found)
|
|
||||||
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
|
||||||
return found_variable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@static_environments.each do |environment|
|
|
||||||
found_variable = lookup_and_evaluate(environment, key, raise_on_not_found: raise_on_not_found)
|
|
||||||
if !found_variable.nil? || @strict_variables && raise_on_not_found
|
|
||||||
return found_variable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def check_overflow
|
def check_overflow
|
||||||
raise StackLevelError, "Nesting too deep".freeze if overflow?
|
raise StackLevelError, "Nesting too deep".freeze if overflow?
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -86,6 +86,14 @@ class VariableTest < Minitest::Test
|
|||||||
assert_equal "Unknown variable 'test'", e.message
|
assert_equal "Unknown variable 'test'", e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_environment_falsy
|
||||||
|
template = Template.parse(%({{ test }}{% assign test = 'bar' %}{{ test }}))
|
||||||
|
template.assigns['test'] = 'foo'
|
||||||
|
assert_equal 'foobar', template.render!
|
||||||
|
assert_equal 'bar', template.render!('test' => nil)
|
||||||
|
assert_equal 'falsebar', template.render!('test' => false)
|
||||||
|
end
|
||||||
|
|
||||||
def test_multiline_variable
|
def test_multiline_variable
|
||||||
assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
|
assert_equal 'worked', Template.parse("{{\ntest\n}}").render!('test' => 'worked')
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user