mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 10:52:48 -07:00
Allow Hash with default value or default proc to be used
This commit is contained in:
+6
-10
@@ -159,16 +159,12 @@ module Liquid
|
|||||||
# fetches an object starting at the local scope and then moving up
|
# fetches an object starting at the local scope and then moving up
|
||||||
# the hierachy
|
# the hierachy
|
||||||
def find_variable(key)
|
def find_variable(key)
|
||||||
@scopes.each do |scope|
|
scope = @scopes[0..-2].find { |s| s.has_key?(key) } || @scopes.last
|
||||||
if scope.has_key?(key)
|
variable = scope[key]
|
||||||
variable = scope[key]
|
variable = scope[key] = variable.call(self) if variable.is_a?(Proc)
|
||||||
variable = scope[key] = variable.call(self) if variable.is_a?(Proc)
|
variable = variable.to_liquid
|
||||||
variable = variable.to_liquid
|
variable.context = self if variable.respond_to?(:context=)
|
||||||
variable.context = self if variable.respond_to?(:context=)
|
return variable
|
||||||
return variable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
nil
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# resolves namespaced queries gracefully.
|
# resolves namespaced queries gracefully.
|
||||||
|
|||||||
@@ -157,4 +157,16 @@ class VariableResolutionTest < Test::Unit::TestCase
|
|||||||
assert_equal 'bazbar', template.render
|
assert_equal 'bazbar', template.render
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_hash_with_default_proc
|
||||||
|
template = Template.parse(%|Hello {{ test }}|)
|
||||||
|
assigns = Hash.new { |h,k| raise "Unknown variable '#{k}'" }
|
||||||
|
assigns['test'] = 'Tobi'
|
||||||
|
assert_equal 'Hello Tobi', template.render!(assigns)
|
||||||
|
assigns.delete('test')
|
||||||
|
e = assert_raises(RuntimeError) {
|
||||||
|
template.render!(assigns)
|
||||||
|
}
|
||||||
|
assert_equal "Unknown variable 'test'", e.message
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
Reference in New Issue
Block a user