mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-15 08:50:45 -07:00
Do not raise when variable is defined but nil when using strict_variables
This commit is contained in:
@@ -171,7 +171,9 @@ module Liquid
|
|||||||
if scope.nil?
|
if scope.nil?
|
||||||
@environments.each do |e|
|
@environments.each do |e|
|
||||||
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
|
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
|
||||||
unless variable.nil?
|
# When lookup returned a value OR there is no value but the lookup also did not raise
|
||||||
|
# then it is the value we are looking for.
|
||||||
|
if !variable.nil? || @strict_variables && raise_on_not_found
|
||||||
scope = e
|
scope = e
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -261,6 +261,15 @@ class TemplateTest < Minitest::Test
|
|||||||
assert_equal 'Liquid error: undefined variable d', t.errors[2].message
|
assert_equal 'Liquid error: undefined variable d', t.errors[2].message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_nil_value_does_not_raise
|
||||||
|
Liquid::Template.error_mode = :strict
|
||||||
|
t = Template.parse("some{{x}}thing")
|
||||||
|
result = t.render!({ 'x' => nil }, strict_variables: true)
|
||||||
|
|
||||||
|
assert_equal 0, t.errors.count
|
||||||
|
assert_equal 'something', result
|
||||||
|
end
|
||||||
|
|
||||||
def test_undefined_variables_raise
|
def test_undefined_variables_raise
|
||||||
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")
|
t = Template.parse("{{x}} {{y}} {{z.a}} {{z.b}} {{z.c.d}}")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user