mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Fix include tag used with strict_variables (#829)
Fixes https://github.com/Shopify/liquid/issues/828
This commit is contained in:
committed by
Dylan Thacker-Smith
parent
22f2cec5de
commit
5149cde5c3
@@ -24,6 +24,7 @@
|
|||||||
* Liquid::Template.register_filter raises when the module overrides registered public methods as private or protected (#705) [Gaurav Chande]
|
* Liquid::Template.register_filter raises when the module overrides registered public methods as private or protected (#705) [Gaurav Chande]
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
* Fix include tag used with strict_variables (#828) [QuickPay]
|
||||||
* Fix map filter when value is a Proc (#672) [Guillaume Malette]
|
* Fix map filter when value is a Proc (#672) [Guillaume Malette]
|
||||||
* Fix truncate filter when value is not a string (#672) [Guillaume Malette]
|
* Fix truncate filter when value is not a string (#672) [Guillaume Malette]
|
||||||
* Fix behaviour of escape filter when input is nil (#665) [Tanel Jakobsoo]
|
* Fix behaviour of escape filter when input is nil (#665) [Tanel Jakobsoo]
|
||||||
|
|||||||
@@ -160,7 +160,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
# Fetches an object starting at the local scope and then moving up the hierachy
|
# Fetches an object starting at the local scope and then moving up the hierachy
|
||||||
def find_variable(key)
|
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) }
|
index = @scopes.find_index { |s| s.key?(key) }
|
||||||
@@ -170,7 +170,7 @@ module Liquid
|
|||||||
|
|
||||||
if scope.nil?
|
if scope.nil?
|
||||||
@environments.each do |e|
|
@environments.each do |e|
|
||||||
variable = lookup_and_evaluate(e, key)
|
variable = lookup_and_evaluate(e, key, raise_on_not_found: raise_on_not_found)
|
||||||
unless variable.nil?
|
unless variable.nil?
|
||||||
scope = e
|
scope = e
|
||||||
break
|
break
|
||||||
@@ -179,7 +179,7 @@ module Liquid
|
|||||||
end
|
end
|
||||||
|
|
||||||
scope ||= @environments.last || @scopes.last
|
scope ||= @environments.last || @scopes.last
|
||||||
variable ||= lookup_and_evaluate(scope, key)
|
variable ||= lookup_and_evaluate(scope, key, raise_on_not_found: raise_on_not_found)
|
||||||
|
|
||||||
variable = variable.to_liquid
|
variable = variable.to_liquid
|
||||||
variable.context = self if variable.respond_to?(:context=)
|
variable.context = self if variable.respond_to?(:context=)
|
||||||
@@ -187,8 +187,8 @@ module Liquid
|
|||||||
variable
|
variable
|
||||||
end
|
end
|
||||||
|
|
||||||
def lookup_and_evaluate(obj, key)
|
def lookup_and_evaluate(obj, key, raise_on_not_found: true)
|
||||||
if @strict_variables && obj.respond_to?(:key?) && !obj.key?(key)
|
if @strict_variables && raise_on_not_found && obj.respond_to?(:key?) && !obj.key?(key)
|
||||||
raise Liquid::UndefinedVariable, "undefined variable #{key}"
|
raise Liquid::UndefinedVariable, "undefined variable #{key}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ module Liquid
|
|||||||
variable = if @variable_name_expr
|
variable = if @variable_name_expr
|
||||||
context.evaluate(@variable_name_expr)
|
context.evaluate(@variable_name_expr)
|
||||||
else
|
else
|
||||||
context.find_variable(template_name)
|
context.find_variable(template_name, raise_on_not_found: false)
|
||||||
end
|
end
|
||||||
|
|
||||||
old_template_name = context.template_name
|
old_template_name = context.template_name
|
||||||
|
|||||||
@@ -235,4 +235,11 @@ class IncludeTagTest < Minitest::Test
|
|||||||
|
|
||||||
assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", "foo" => { 'title' => 'Draft 151cm' }
|
assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", "foo" => { 'title' => 'Draft 151cm' }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_including_with_strict_variables
|
||||||
|
template = Liquid::Template.parse("{% include 'simple' %}", error_mode: :warn)
|
||||||
|
template.render(nil, strict_variables: true)
|
||||||
|
|
||||||
|
assert_equal [], template.errors
|
||||||
|
end
|
||||||
end # IncludeTagTest
|
end # IncludeTagTest
|
||||||
|
|||||||
Reference in New Issue
Block a user