mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 08:15:41 +03:00
Compare commits
6 Commits
context-dr
...
render-for
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56a1034ac2 | ||
|
|
1c3dcb0ddc | ||
|
|
1223444738 | ||
|
|
2bfeed2b00 | ||
|
|
04b800d768 | ||
|
|
f1d62978ef |
@@ -421,13 +421,20 @@ 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
|
||||||
|
|||||||
@@ -16,18 +16,20 @@ module Liquid
|
|||||||
# {% include 'product' for products %}
|
# {% include 'product' for products %}
|
||||||
#
|
#
|
||||||
class Include < Tag
|
class Include < Tag
|
||||||
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o
|
SYNTAX = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||||
|
Syntax = SYNTAX
|
||||||
|
|
||||||
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
||||||
|
|
||||||
def initialize(tag_name, markup, options)
|
def initialize(tag_name, markup, options)
|
||||||
super
|
super
|
||||||
|
|
||||||
if markup =~ Syntax
|
if markup =~ SYNTAX
|
||||||
|
|
||||||
template_name = Regexp.last_match(1)
|
template_name = Regexp.last_match(1)
|
||||||
variable_name = Regexp.last_match(3)
|
variable_name = Regexp.last_match(3)
|
||||||
|
|
||||||
|
@alias_name = Regexp.last_match(5)
|
||||||
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||||
@template_name_expr = Expression.parse(template_name)
|
@template_name_expr = Expression.parse(template_name)
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
@@ -54,7 +56,7 @@ module Liquid
|
|||||||
parse_context: parse_context
|
parse_context: parse_context
|
||||||
)
|
)
|
||||||
|
|
||||||
context_variable_name = template_name.split('/').last
|
context_variable_name = @alias_name || template_name.split('/').last
|
||||||
|
|
||||||
variable = if @variable_name_expr
|
variable = if @variable_name_expr
|
||||||
context.evaluate(@variable_name_expr)
|
context.evaluate(@variable_name_expr)
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class Render < Tag
|
class Render < Tag
|
||||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||||
|
|
||||||
disable_tags "include"
|
disable_tags "include"
|
||||||
|
|
||||||
@@ -14,7 +14,10 @@ module Liquid
|
|||||||
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
|
||||||
|
|
||||||
template_name = Regexp.last_match(1)
|
template_name = Regexp.last_match(1)
|
||||||
|
variable_name = Regexp.last_match(3)
|
||||||
|
|
||||||
|
@alias_name = Regexp.last_match(5)
|
||||||
|
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||||
@template_name_expr = Expression.parse(template_name)
|
@template_name_expr = Expression.parse(template_name)
|
||||||
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
@@ -38,13 +41,28 @@ module Liquid
|
|||||||
parse_context: parse_context
|
parse_context: parse_context
|
||||||
)
|
)
|
||||||
|
|
||||||
|
context_variable_name = @alias_name || template_name.split('/').last
|
||||||
|
|
||||||
|
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?
|
||||||
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
|
||||||
|
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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ class TestFileSystem
|
|||||||
when "product"
|
when "product"
|
||||||
"Product: {{ product.title }} "
|
"Product: {{ product.title }} "
|
||||||
|
|
||||||
|
when "product_alias"
|
||||||
|
"Product: {{ product.title }} "
|
||||||
|
|
||||||
when "locale_variables"
|
when "locale_variables"
|
||||||
"Locale: {{echo1}} {{echo2}}"
|
"Locale: {{echo1}} {{echo2}}"
|
||||||
|
|
||||||
@@ -91,6 +94,16 @@ class IncludeTagTest < Minitest::Test
|
|||||||
"{% include 'product' with products[0] %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
"{% include 'product' with products[0] %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_include_tag_with_alias
|
||||||
|
assert_template_result("Product: Draft 151cm ",
|
||||||
|
"{% include 'product_alias' with products[0] as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_include_tag_for_alias
|
||||||
|
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
||||||
|
"{% include 'product_alias' for products as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
|
end
|
||||||
|
|
||||||
def test_include_tag_with_default_name
|
def test_include_tag_with_default_name
|
||||||
assert_template_result("Product: Draft 151cm ",
|
assert_template_result("Product: Draft 151cm ",
|
||||||
"{% include 'product' %}", "product" => { 'title' => 'Draft 151cm' })
|
"{% include 'product' %}", "product" => { 'title' => 'Draft 151cm' })
|
||||||
|
|||||||
@@ -165,4 +165,53 @@ class RenderTagTest < Minitest::Test
|
|||||||
|
|
||||||
assert_template_result('include usage is not allowed in this contextinclude usage is not allowed in this context', '{% render "nested_render_with_sibling_include" %}')
|
assert_template_result('include usage is not allowed in this contextinclude usage is not allowed in this context', '{% render "nested_render_with_sibling_include" %}')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_render_tag_with
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'product' => "Product: {{ product.title }} ",
|
||||||
|
'product_alias' => "Product: {{ product.title }} ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_template_result("Product: Draft 151cm ",
|
||||||
|
"{% render 'product' with products[0] %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_tag_with_alias
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'product' => "Product: {{ product.title }} ",
|
||||||
|
'product_alias' => "Product: {{ product.title }} ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_template_result("Product: Draft 151cm ",
|
||||||
|
"{% render 'product_alias' with products[0] as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_tag_for_alias
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'product' => "Product: {{ product.title }} ",
|
||||||
|
'product_alias' => "Product: {{ product.title }} ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
||||||
|
"{% render 'product_alias' for products as product %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_render_tag_for
|
||||||
|
Liquid::Template.file_system = StubFileSystem.new(
|
||||||
|
'product' => "Product: {{ product.title }} ",
|
||||||
|
'product_alias' => "Product: {{ product.title }} ",
|
||||||
|
)
|
||||||
|
|
||||||
|
assert_template_result("Product: Draft 151cm Product: Element 155cm ",
|
||||||
|
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
|
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