mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 19:00:39 -07:00
Use Expression.parse and Context#evaluate in the Include class.
This commit is contained in:
@@ -12,7 +12,7 @@ module Liquid
|
|||||||
|
|
||||||
class Include < Tag
|
class Include < Tag
|
||||||
def render_with_profiling(context)
|
def render_with_profiling(context)
|
||||||
Profiler.profile_children(@template_name) do
|
Profiler.profile_children("'#{context.evaluate(@template_name)}'") do
|
||||||
render_without_profiling(context)
|
render_without_profiling(context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
+14
-11
@@ -22,12 +22,16 @@ module Liquid
|
|||||||
|
|
||||||
if markup =~ Syntax
|
if markup =~ Syntax
|
||||||
|
|
||||||
@template_name = $1
|
template_name = $1
|
||||||
@variable_name = $3
|
variable_name = $3
|
||||||
|
|
||||||
|
@variable_name = Expression.parse(variable_name || template_name[1..-2])
|
||||||
|
@context_variable_name = template_name[1..-2].split('/'.freeze).last
|
||||||
|
@template_name = Expression.parse(template_name)
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
|
|
||||||
markup.scan(TagAttributes) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@attributes[key] = value
|
@attributes[key] = Expression.parse(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
else
|
else
|
||||||
@@ -40,21 +44,20 @@ module Liquid
|
|||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
partial = load_cached_partial(context)
|
partial = load_cached_partial(context)
|
||||||
variable = context[@variable_name || @template_name[1..-2]]
|
variable = context.evaluate(@variable_name)
|
||||||
|
|
||||||
context.stack do
|
context.stack do
|
||||||
@attributes.each do |key, value|
|
@attributes.each do |key, value|
|
||||||
context[key] = context[value]
|
context[key] = context.evaluate(value)
|
||||||
end
|
end
|
||||||
|
|
||||||
context_variable_name = @template_name[1..-2].split('/'.freeze).last
|
|
||||||
if variable.is_a?(Array)
|
if variable.is_a?(Array)
|
||||||
variable.collect do |var|
|
variable.collect do |var|
|
||||||
context[context_variable_name] = var
|
context[@context_variable_name] = var
|
||||||
partial.render(context)
|
partial.render(context)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
context[context_variable_name] = variable
|
context[@context_variable_name] = variable
|
||||||
partial.render(context)
|
partial.render(context)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -63,7 +66,7 @@ module Liquid
|
|||||||
private
|
private
|
||||||
def load_cached_partial(context)
|
def load_cached_partial(context)
|
||||||
cached_partials = context.registers[:cached_partials] || {}
|
cached_partials = context.registers[:cached_partials] || {}
|
||||||
template_name = context[@template_name]
|
template_name = context.evaluate(@template_name)
|
||||||
|
|
||||||
if cached = cached_partials[template_name]
|
if cached = cached_partials[template_name]
|
||||||
return cached
|
return cached
|
||||||
@@ -81,9 +84,9 @@ module Liquid
|
|||||||
# make read_template_file call backwards-compatible.
|
# make read_template_file call backwards-compatible.
|
||||||
case file_system.method(:read_template_file).arity
|
case file_system.method(:read_template_file).arity
|
||||||
when 1
|
when 1
|
||||||
file_system.read_template_file(context[@template_name])
|
file_system.read_template_file(context.evaluate(@template_name))
|
||||||
when 2
|
when 2
|
||||||
file_system.read_template_file(context[@template_name], context)
|
file_system.read_template_file(context.evaluate(@template_name), context)
|
||||||
else
|
else
|
||||||
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
|
raise ArgumentError, "file_system.read_template_file expects two parameters: (template_name, context)"
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user