mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
8 Commits
render-wit
...
render-for
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56a1034ac2 | ||
|
|
1c3dcb0ddc | ||
|
|
1223444738 | ||
|
|
2bfeed2b00 | ||
|
|
04b800d768 | ||
|
|
f1d62978ef | ||
|
|
ffadc64f28 | ||
|
|
fefee4c675 |
@@ -46,7 +46,7 @@ module Liquid
|
|||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
class Timing
|
class Timing
|
||||||
attr_reader :code, :partial, :line_number, :children
|
attr_reader :code, :partial, :line_number, :children, :total_time, :self_time
|
||||||
|
|
||||||
def initialize(node, partial)
|
def initialize(node, partial)
|
||||||
@code = node.respond_to?(:raw) ? node.raw : node
|
@code = node.respond_to?(:raw) ? node.raw : node
|
||||||
@@ -65,6 +65,17 @@ module Liquid
|
|||||||
|
|
||||||
def finish
|
def finish
|
||||||
@end_time = Time.now
|
@end_time = Time.now
|
||||||
|
@total_time = @end_time - @start_time
|
||||||
|
|
||||||
|
if @children.empty?
|
||||||
|
@self_time = @total_time
|
||||||
|
else
|
||||||
|
total_children_time = 0
|
||||||
|
@children.each do |child|
|
||||||
|
total_children_time += child.total_time
|
||||||
|
end
|
||||||
|
@self_time = @total_time - total_children_time
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_time
|
def render_time
|
||||||
@@ -98,8 +109,8 @@ module Liquid
|
|||||||
Thread.current[:liquid_profiler]
|
Thread.current[:liquid_profiler]
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize
|
def initialize(partial_name = "<root>")
|
||||||
@partial_stack = ["<root>"]
|
@partial_stack = [partial_name]
|
||||||
|
|
||||||
@root_timing = Timing.new("", current_partial)
|
@root_timing = Timing.new("", current_partial)
|
||||||
@timing_stack = [@root_timing]
|
@timing_stack = [@root_timing]
|
||||||
|
|||||||
@@ -128,13 +128,13 @@ module Liquid
|
|||||||
|
|
||||||
# Join elements of the array with certain character between them
|
# Join elements of the array with certain character between them
|
||||||
def join(input, glue = ' ')
|
def join(input, glue = ' ')
|
||||||
InputIterator.new(input).join(glue)
|
InputIterator.new(input, context).join(glue)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sort elements of the array
|
# Sort elements of the array
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort(input, property = nil)
|
def sort(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return [] if ary.empty?
|
||||||
|
|
||||||
@@ -154,7 +154,7 @@ module Liquid
|
|||||||
# Sort elements of an array ignoring case if strings
|
# Sort elements of an array ignoring case if strings
|
||||||
# provide optional property with which to sort an array of hashes or drops
|
# provide optional property with which to sort an array of hashes or drops
|
||||||
def sort_natural(input, property = nil)
|
def sort_natural(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
return [] if ary.empty?
|
return [] if ary.empty?
|
||||||
|
|
||||||
@@ -174,7 +174,7 @@ module Liquid
|
|||||||
# Filter the elements of an array to those with a certain property value.
|
# Filter the elements of an array to those with a certain property value.
|
||||||
# By default the target is any truthy value.
|
# By default the target is any truthy value.
|
||||||
def where(input, property, target_value = nil)
|
def where(input, property, target_value = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
if ary.empty?
|
if ary.empty?
|
||||||
[]
|
[]
|
||||||
@@ -196,7 +196,7 @@ module Liquid
|
|||||||
# Remove duplicate elements from an array
|
# Remove duplicate elements from an array
|
||||||
# provide optional property with which to determine uniqueness
|
# provide optional property with which to determine uniqueness
|
||||||
def uniq(input, property = nil)
|
def uniq(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.uniq
|
ary.uniq
|
||||||
@@ -213,13 +213,13 @@ module Liquid
|
|||||||
|
|
||||||
# Reverse the elements of an array
|
# Reverse the elements of an array
|
||||||
def reverse(input)
|
def reverse(input)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
ary.reverse
|
ary.reverse
|
||||||
end
|
end
|
||||||
|
|
||||||
# map/collect on a given property
|
# map/collect on a given property
|
||||||
def map(input, property)
|
def map(input, property)
|
||||||
InputIterator.new(input).map do |e|
|
InputIterator.new(input, context).map do |e|
|
||||||
e = e.call if e.is_a?(Proc)
|
e = e.call if e.is_a?(Proc)
|
||||||
|
|
||||||
if property == "to_liquid"
|
if property == "to_liquid"
|
||||||
@@ -236,7 +236,7 @@ module Liquid
|
|||||||
# Remove nils within an array
|
# Remove nils within an array
|
||||||
# provide optional property with which to check for nil
|
# provide optional property with which to check for nil
|
||||||
def compact(input, property = nil)
|
def compact(input, property = nil)
|
||||||
ary = InputIterator.new(input)
|
ary = InputIterator.new(input, context)
|
||||||
|
|
||||||
if property.nil?
|
if property.nil?
|
||||||
ary.compact
|
ary.compact
|
||||||
@@ -280,7 +280,7 @@ module Liquid
|
|||||||
unless array.respond_to?(:to_ary)
|
unless array.respond_to?(:to_ary)
|
||||||
raise ArgumentError, "concat filter requires an array argument"
|
raise ArgumentError, "concat filter requires an array argument"
|
||||||
end
|
end
|
||||||
InputIterator.new(input).concat(array)
|
InputIterator.new(input, context).concat(array)
|
||||||
end
|
end
|
||||||
|
|
||||||
# prepend a string to another
|
# prepend a string to another
|
||||||
@@ -421,17 +421,26 @@ module Liquid
|
|||||||
result.is_a?(BigDecimal) ? result.to_f : result
|
result.is_a?(BigDecimal) ? result.to_f : result
|
||||||
end
|
end
|
||||||
|
|
||||||
def default(input, default_value = '')
|
# Set a default value when the input is nil, false or empty
|
||||||
if !input || input.respond_to?(:empty?) && input.empty?
|
#
|
||||||
Usage.increment("default_filter_received_false_value") if input == false # See https://github.com/Shopify/liquid/issues/1127
|
# Example:
|
||||||
default_value
|
# {{ product.title | default: "No Title" }}
|
||||||
else
|
#
|
||||||
input
|
# Use `allow_false` when an input should only be tested against nil or empty and not false.
|
||||||
end
|
#
|
||||||
|
# Example:
|
||||||
|
# {{ product.title | default: "No Title", allow_false: true }}
|
||||||
|
#
|
||||||
|
def default(input, default_value = '', options = {})
|
||||||
|
options = {} unless options.is_a?(Hash)
|
||||||
|
false_check = options['allow_false'] ? input.nil? : !input
|
||||||
|
false_check || (input.respond_to?(:empty?) && input.empty?) ? default_value : input
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
attr_reader :context
|
||||||
|
|
||||||
def raise_property_error(property)
|
def raise_property_error(property)
|
||||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
||||||
end
|
end
|
||||||
@@ -460,7 +469,8 @@ module Liquid
|
|||||||
class InputIterator
|
class InputIterator
|
||||||
include Enumerable
|
include Enumerable
|
||||||
|
|
||||||
def initialize(input)
|
def initialize(input, context)
|
||||||
|
@context = context
|
||||||
@input = if input.is_a?(Array)
|
@input = if input.is_a?(Array)
|
||||||
input.flatten
|
input.flatten
|
||||||
elsif input.is_a?(Hash)
|
elsif input.is_a?(Hash)
|
||||||
@@ -499,6 +509,7 @@ module Liquid
|
|||||||
|
|
||||||
def each
|
def each
|
||||||
@input.each do |e|
|
@input.each do |e|
|
||||||
|
e.context = @context if e.respond_to?(:context=)
|
||||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -254,7 +254,7 @@ module Liquid
|
|||||||
if @profiling && !context.partial
|
if @profiling && !context.partial
|
||||||
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
||||||
|
|
||||||
@profiler = Profiler.new
|
@profiler = Profiler.new(context.template_name)
|
||||||
@profiler.start
|
@profiler.start
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
|||||||
@@ -179,6 +179,11 @@ class DropsTest < Minitest::Test
|
|||||||
assert_equal(' carrot ', output)
|
assert_equal(' carrot ', output)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_context_drop_array_with_map
|
||||||
|
output = Liquid::Template.parse(' {{ contexts | map: "bar" }} ').render!('contexts' => [ContextDrop.new, ContextDrop.new], 'bar' => "carrot")
|
||||||
|
assert_equal(' carrotcarrot ', output)
|
||||||
|
end
|
||||||
|
|
||||||
def test_nested_context_drop
|
def test_nested_context_drop
|
||||||
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
||||||
assert_equal(' monkey ', output)
|
assert_equal(' monkey ', output)
|
||||||
|
|||||||
@@ -153,4 +153,19 @@ class RenderProfilingTest < Minitest::Test
|
|||||||
# Will profile each invocation of the for block
|
# Will profile each invocation of the for block
|
||||||
assert_equal(2, t.profiler[0].children.length)
|
assert_equal(2, t.profiler[0].children.length)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_profiling_supports_self_time
|
||||||
|
t = Template.parse("{% for item in collection %} {{ item }} {% endfor %}", profile: true)
|
||||||
|
t.render!("collection" => ["one", "two"])
|
||||||
|
leaf = t.profiler[0].children[0]
|
||||||
|
|
||||||
|
assert_operator leaf.self_time, :>, 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_profiling_supports_total_time
|
||||||
|
t = Template.parse("{% if true %} {% increment test %} {{ test }} {% endif %}", profile: true)
|
||||||
|
t.render!
|
||||||
|
|
||||||
|
assert_operator t.profiler[0].total_time, :>, 0
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -685,6 +685,17 @@ class StandardFiltersTest < Minitest::Test
|
|||||||
assert_equal("bar", @filters.default(false, "bar"))
|
assert_equal("bar", @filters.default(false, "bar"))
|
||||||
assert_equal("bar", @filters.default([], "bar"))
|
assert_equal("bar", @filters.default([], "bar"))
|
||||||
assert_equal("bar", @filters.default({}, "bar"))
|
assert_equal("bar", @filters.default({}, "bar"))
|
||||||
|
assert_template_result('bar', "{{ false | default: 'bar' }}")
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_default_handle_false
|
||||||
|
assert_equal("foo", @filters.default("foo", "bar", "allow_false" => true))
|
||||||
|
assert_equal("bar", @filters.default(nil, "bar", "allow_false" => true))
|
||||||
|
assert_equal("bar", @filters.default("", "bar", "allow_false" => true))
|
||||||
|
assert_equal(false, @filters.default(false, "bar", "allow_false" => true))
|
||||||
|
assert_equal("bar", @filters.default([], "bar", "allow_false" => true))
|
||||||
|
assert_equal("bar", @filters.default({}, "bar", "allow_false" => true))
|
||||||
|
assert_template_result('false', "{{ false | default: 'bar', allow_false: true }}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_cannot_access_private_methods
|
def test_cannot_access_private_methods
|
||||||
|
|||||||
@@ -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