mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
9 Commits
render-wit
...
default-fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12d047aef0 | ||
|
|
c109615da7 | ||
|
|
a04c23eb38 | ||
|
|
afbe7b930f | ||
|
|
f7076e1682 | ||
|
|
135a4bc84d | ||
|
|
7ebf1aeef2 | ||
|
|
9bd5362ca1 | ||
|
|
e3fba19de0 |
@@ -421,13 +421,20 @@ module Liquid
|
||||
result.is_a?(BigDecimal) ? result.to_f : result
|
||||
end
|
||||
|
||||
def default(input, default_value = '')
|
||||
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
|
||||
default_value
|
||||
else
|
||||
input
|
||||
end
|
||||
# Set a default value when the input is nil, false or empty
|
||||
#
|
||||
# Example:
|
||||
# {{ product.title | default: "No Title" }}
|
||||
#
|
||||
# Use `allow_false` when an input should only be tested against nil or empty and not false.
|
||||
#
|
||||
# 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
|
||||
|
||||
private
|
||||
|
||||
@@ -16,20 +16,18 @@ module Liquid
|
||||
# {% include 'product' for products %}
|
||||
#
|
||||
class Include < Tag
|
||||
SYNTAX = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
Syntax = SYNTAX
|
||||
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/o
|
||||
|
||||
attr_reader :template_name_expr, :variable_name_expr, :attributes
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
if markup =~ SYNTAX
|
||||
if markup =~ Syntax
|
||||
|
||||
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)
|
||||
@attributes = {}
|
||||
@@ -56,7 +54,7 @@ module Liquid
|
||||
parse_context: parse_context
|
||||
)
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
context_variable_name = template_name.split('/').last
|
||||
|
||||
variable = if @variable_name_expr
|
||||
context.evaluate(@variable_name_expr)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
module Liquid
|
||||
class Render < Tag
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||
|
||||
disable_tags "include"
|
||||
|
||||
@@ -14,10 +14,7 @@ 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)
|
||||
|
||||
@alias_name = Regexp.last_match(5)
|
||||
@variable_name_expr = variable_name ? Expression.parse(variable_name) : nil
|
||||
@template_name_expr = Expression.parse(template_name)
|
||||
|
||||
@attributes = {}
|
||||
@@ -41,21 +38,13 @@ module Liquid
|
||||
parse_context: parse_context
|
||||
)
|
||||
|
||||
context_variable_name = @alias_name || template_name.split('/').last
|
||||
|
||||
render_partial_func = ->(var) {
|
||||
inner_context = context.new_isolated_subcontext
|
||||
inner_context.template_name = template_name
|
||||
inner_context.partial = true
|
||||
@attributes.each do |key, value|
|
||||
inner_context[key] = context.evaluate(value)
|
||||
end
|
||||
inner_context[context_variable_name] = var unless var.nil?
|
||||
partial.render_to_output_buffer(inner_context, output)
|
||||
}
|
||||
|
||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||
variable.is_a?(Array) ? variable.each(&render_partial_func) : render_partial_func.call(variable)
|
||||
inner_context = context.new_isolated_subcontext
|
||||
inner_context.template_name = template_name
|
||||
inner_context.partial = true
|
||||
@attributes.each do |key, value|
|
||||
inner_context[key] = context.evaluate(value)
|
||||
end
|
||||
partial.render_to_output_buffer(inner_context, output)
|
||||
|
||||
output
|
||||
end
|
||||
|
||||
@@ -685,6 +685,17 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_equal("bar", @filters.default(false, "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
|
||||
|
||||
def test_cannot_access_private_methods
|
||||
|
||||
@@ -8,9 +8,6 @@ class TestFileSystem
|
||||
when "product"
|
||||
"Product: {{ product.title }} "
|
||||
|
||||
when "product_alias"
|
||||
"Product: {{ product.title }} "
|
||||
|
||||
when "locale_variables"
|
||||
"Locale: {{echo1}} {{echo2}}"
|
||||
|
||||
@@ -94,16 +91,6 @@ class IncludeTagTest < Minitest::Test
|
||||
"{% include 'product' with products[0] %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||
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
|
||||
assert_template_result("Product: Draft 151cm ",
|
||||
"{% include 'product' %}", "product" => { 'title' => 'Draft 151cm' })
|
||||
|
||||
@@ -165,44 +165,4 @@ 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" %}')
|
||||
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
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user