Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

828 fix content variable error #829

Merged
merged 2 commits into from Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions History.md
Expand Up @@ -22,6 +22,7 @@
* Liquid::Template.register_filter raises when the module overrides registered public methods as private or protected (#705) [Gaurav Chande]

### Fixed
* Fix include tag used with strict_variables (#828) [QuickPay]
* Fix map filter when value is a Proc (#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]
Expand Down
10 changes: 5 additions & 5 deletions lib/liquid/context.rb
Expand Up @@ -180,7 +180,7 @@ def evaluate(object)
end

# 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
# path and find_index() is optimized in MRI to reduce object allocation
index = @scopes.find_index { |s| s.key?(key) }
Expand All @@ -190,7 +190,7 @@ def find_variable(key)

if scope.nil?
@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?
scope = e
break
Expand All @@ -199,16 +199,16 @@ def find_variable(key)
end

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.context = self if variable.respond_to?(:context=)

variable
end

def lookup_and_evaluate(obj, key)
if @strict_variables && obj.respond_to?(:key?) && !obj.key?(key)
def lookup_and_evaluate(obj, key, raise_on_not_found: true)
if @strict_variables && raise_on_not_found && obj.respond_to?(:key?) && !obj.key?(key)
raise Liquid::UndefinedVariable, "undefined variable #{key}"
end

Expand Down
2 changes: 1 addition & 1 deletion lib/liquid/tags/include.rb
Expand Up @@ -50,7 +50,7 @@ def render(context)
variable = if @variable_name_expr
context.evaluate(@variable_name_expr)
else
context.find_variable(template_name)
context.find_variable(template_name, raise_on_not_found: false)
end

old_template_name = context.template_name
Expand Down
7 changes: 7 additions & 0 deletions test/integration/tags/include_tag_test.rb
Expand Up @@ -235,4 +235,11 @@ def test_including_via_variable_value

assert_template_result "Product: Draft 151cm ", "{% assign page = 'product' %}{% include page for foo %}", "foo" => { 'title' => 'Draft 151cm' }
end

def test_including_with_strict_variables
template = Liquid::Template.parse("{% include 'simple' %}", error_mode: :warn)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this template ("simple") exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it does not need to exists to show the issue under test. In fact, AFAIR I added the template but removed it again before comitting, since it was not needed here.

template.render(nil, strict_variables: true)

assert_equal [], template.errors
end
end # IncludeTagTest