mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 08:15:41 +03:00
Compare commits
3 Commits
fix-flaky-
...
render-for
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56a1034ac2 | ||
|
|
1c3dcb0ddc | ||
|
|
1223444738 |
@@ -43,19 +43,26 @@ module Liquid
|
|||||||
|
|
||||||
context_variable_name = @alias_name || template_name.split('/').last
|
context_variable_name = @alias_name || template_name.split('/').last
|
||||||
|
|
||||||
render_partial_func = ->(var) {
|
render_partial_func = ->(var, forloop) {
|
||||||
inner_context = context.new_isolated_subcontext
|
inner_context = context.new_isolated_subcontext
|
||||||
inner_context.template_name = template_name
|
inner_context.template_name = template_name
|
||||||
inner_context.partial = true
|
inner_context.partial = true
|
||||||
|
inner_context['forloop'] = forloop if forloop
|
||||||
@attributes.each do |key, value|
|
@attributes.each do |key, value|
|
||||||
inner_context[key] = context.evaluate(value)
|
inner_context[key] = context.evaluate(value)
|
||||||
end
|
end
|
||||||
inner_context[context_variable_name] = var unless var.nil?
|
inner_context[context_variable_name] = var unless var.nil?
|
||||||
partial.render_to_output_buffer(inner_context, output)
|
partial.render_to_output_buffer(inner_context, output)
|
||||||
|
forloop&.send(:increment!)
|
||||||
}
|
}
|
||||||
|
|
||||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||||
variable.is_a?(Array) ? variable.each(&render_partial_func) : render_partial_func.call(variable)
|
if variable.is_a?(Array)
|
||||||
|
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
||||||
|
variable.each { |var| render_partial_func.call(var, forloop) }
|
||||||
|
else
|
||||||
|
render_partial_func.call(variable, nil)
|
||||||
|
end
|
||||||
|
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -154,18 +154,18 @@ class RenderProfilingTest < Minitest::Test
|
|||||||
assert_equal(2, t.profiler[0].children.length)
|
assert_equal(2, t.profiler[0].children.length)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_total_time_equals_self_time_in_leaf
|
def test_profiling_supports_self_time
|
||||||
t = Template.parse("{% for item in collection %} {{ item }} {% endfor %}", profile: true)
|
t = Template.parse("{% for item in collection %} {{ item }} {% endfor %}", profile: true)
|
||||||
t.render!("collection" => ["one", "two"])
|
t.render!("collection" => ["one", "two"])
|
||||||
leaf = t.profiler[0].children[0]
|
leaf = t.profiler[0].children[0]
|
||||||
|
|
||||||
assert_equal leaf.total_time, leaf.self_time
|
assert_operator leaf.self_time, :>, 0
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_total_time_greater_than_self_time_in_node
|
def test_profiling_supports_total_time
|
||||||
t = Template.parse("{% if true %} {% increment test %} {{ test }} {% endif %}", profile: true)
|
t = Template.parse("{% if true %} {% increment test %} {{ test }} {% endif %}", profile: true)
|
||||||
t.render!
|
t.render!
|
||||||
|
|
||||||
assert_operator t.profiler[0].total_time, :>, t.profiler[0].self_time
|
assert_operator t.profiler[0].total_time, :>, 0
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -205,4 +205,13 @@ class RenderTagTest < Minitest::Test
|
|||||||
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
||||||
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_tag_forloop
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'product' => "Product: {{ product.title }} {% if forloop.first %}first{% endif %} {% if forloop.last %}last{% endif %} index:{{ forloop.index }} ",
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user