mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
3 Commits
fix-consta
...
sfr-verifi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
974080c2cf | ||
|
|
e52475a7cd | ||
|
|
57c9cf64eb |
@@ -50,19 +50,7 @@ module Liquid
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||
|
||||
partial = PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
context.evaluate(@variable_name_expr)
|
||||
else
|
||||
context.find_variable(template_name, raise_on_not_found: false)
|
||||
end
|
||||
partial = load_partial(template_name, context, parse_context)
|
||||
|
||||
old_template_name = context.template_name
|
||||
old_partial = context.partial
|
||||
@@ -71,7 +59,15 @@ module Liquid
|
||||
context.partial = true
|
||||
context.stack do
|
||||
@attributes.each do |key, value|
|
||||
context[key] = context.evaluate(value)
|
||||
context[key] = evaluate(context, value)
|
||||
end
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
evaluate(context, @variable_name_expr)
|
||||
else
|
||||
find_variable(context, template_name, raise_on_not_found: false)
|
||||
end
|
||||
|
||||
if variable.is_a?(Array)
|
||||
@@ -103,6 +99,24 @@ module Liquid
|
||||
] + @node.attributes.values
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def evaluate(context, value)
|
||||
context.evaluate(value)
|
||||
end
|
||||
|
||||
def find_variable(context, *args)
|
||||
context.find_variable(*args)
|
||||
end
|
||||
|
||||
def load_partial(template_name, context, parse_context)
|
||||
PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('include', Include)
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
|
||||
module Liquid
|
||||
class Render < Tag
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
FOR = 'for'
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
|
||||
disable_tags "include"
|
||||
|
||||
@@ -14,11 +15,13 @@ module Liquid
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
||||
|
||||
template_name = Regexp.last_match(1)
|
||||
variable_name = Regexp.last_match(3)
|
||||
with_or_for = Regexp.last_match(3)
|
||||
variable_name = Regexp.last_match(4)
|
||||
|
||||
@alias_name = Regexp.last_match(5)
|
||||
@alias_name = Regexp.last_match(6)
|
||||
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
@for = (with_or_for == FOR)
|
||||
|
||||
@attributes = {}
|
||||
markup.scan(TagAttributes) do |key, value|
|
||||
@@ -35,11 +38,7 @@ module Liquid
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||
|
||||
partial = PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
partial = load_partial(template_name, context, parse_context)
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
@@ -50,15 +49,15 @@ module Liquid
|
||||
inner_context['forloop'] = forloop if forloop
|
||||
|
||||
@attributes.each do |key, value|
|
||||
inner_context[key] = context.evaluate(value)
|
||||
inner_context[key] = evaluate(context, value)
|
||||
end
|
||||
inner_context[context_variable_name] = var unless var.nil?
|
||||
partial.render_to_output_buffer(inner_context, output)
|
||||
forloop&.send(:increment!)
|
||||
}
|
||||
|
||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||
if variable.is_a?(Array)
|
||||
variable = @variable_name_expr ? evaluate(context, @variable_name_expr) : nil
|
||||
if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
|
||||
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
||||
variable.each { |var| render_partial_func.call(var, forloop) }
|
||||
else
|
||||
@@ -75,6 +74,20 @@ module Liquid
|
||||
] + @node.attributes.values
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def evaluate(context, value)
|
||||
context.evaluate(value)
|
||||
end
|
||||
|
||||
def load_partial(template_name, context, parse_context)
|
||||
PartialCache.load(
|
||||
template_name,
|
||||
context: context,
|
||||
parse_context: parse_context
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
Template.register_tag('render', Render)
|
||||
|
||||
@@ -214,4 +214,22 @@ class RenderTagTest < Minitest::Test
|
||||
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
|
||||
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||
end
|
||||
|
||||
def test_render_tag_for_drop
|
||||
Liquid::Template.file_system = StubFileSystem.new(
|
||||
'loop' => "{{ value.foo }}",
|
||||
)
|
||||
|
||||
assert_template_result("123",
|
||||
"{% render 'loop' for loop as value %}", "loop" => TestEnumerable.new)
|
||||
end
|
||||
|
||||
def test_render_tag_with_drop
|
||||
Liquid::Template.file_system = StubFileSystem.new(
|
||||
'loop' => "{{ value }}",
|
||||
)
|
||||
|
||||
assert_template_result("TestEnumerable",
|
||||
"{% render 'loop' with loop as value %}", "loop" => TestEnumerable.new)
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user