mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 01:05:40 +03:00
Compare commits
2 Commits
sfr-verifi
...
render-for
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
56a1034ac2 | ||
|
|
1c3dcb0ddc |
@@ -892,7 +892,7 @@ Lint/FormatParameterMismatch:
|
|||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Lint/HandleExceptions:
|
Lint/HandleExceptions:
|
||||||
AllowComments: true
|
Enabled: true
|
||||||
|
|
||||||
Lint/ImplicitStringConcatenation:
|
Lint/ImplicitStringConcatenation:
|
||||||
Description: Checks for adjacent string literals on the same line, which could
|
Description: Checks for adjacent string literals on the same line, which could
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ module Liquid
|
|||||||
@left = left
|
@left = left
|
||||||
@operator = operator
|
@operator = operator
|
||||||
@right = right
|
@right = right
|
||||||
|
|
||||||
@child_relation = nil
|
@child_relation = nil
|
||||||
@child_condition = nil
|
@child_condition = nil
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -7,10 +7,8 @@ module Liquid
|
|||||||
|
|
||||||
def initialize(options = {})
|
def initialize(options = {})
|
||||||
@template_options = options ? options.dup : {}
|
@template_options = options ? options.dup : {}
|
||||||
|
|
||||||
@locale = @template_options[:locale] ||= I18n.new
|
@locale = @template_options[:locale] ||= I18n.new
|
||||||
@warnings = []
|
@warnings = []
|
||||||
|
|
||||||
self.depth = 0
|
self.depth = 0
|
||||||
self.partial = false
|
self.partial = false
|
||||||
end
|
end
|
||||||
@@ -22,7 +20,6 @@ module Liquid
|
|||||||
def partial=(value)
|
def partial=(value)
|
||||||
@partial = value
|
@partial = value
|
||||||
@options = value ? partial_options : @template_options
|
@options = value ? partial_options : @template_options
|
||||||
|
|
||||||
@error_mode = @options[:error_mode] || Template.error_mode
|
@error_mode = @options[:error_mode] || Template.error_mode
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ module Liquid
|
|||||||
|
|
||||||
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
|
file_system = (context.registers[:file_system] ||= Liquid::Template.file_system)
|
||||||
source = file_system.read_template_file(template_name)
|
source = file_system.read_template_file(template_name)
|
||||||
|
|
||||||
parse_context.partial = true
|
parse_context.partial = true
|
||||||
|
|
||||||
partial = Liquid::Template.parse(source, parse_context)
|
partial = Liquid::Template.parse(source, parse_context)
|
||||||
|
|||||||
@@ -78,12 +78,9 @@ module Liquid
|
|||||||
return if input.nil?
|
return if input.nil?
|
||||||
input_str = input.to_s
|
input_str = input.to_s
|
||||||
length = Utils.to_integer(length)
|
length = Utils.to_integer(length)
|
||||||
|
|
||||||
truncate_string_str = truncate_string.to_s
|
truncate_string_str = truncate_string.to_s
|
||||||
|
|
||||||
l = length - truncate_string_str.length
|
l = length - truncate_string_str.length
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
|
|
||||||
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
|
input_str.length > length ? input_str[0...l].concat(truncate_string_str) : input_str
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -91,10 +88,8 @@ module Liquid
|
|||||||
return if input.nil?
|
return if input.nil?
|
||||||
wordlist = input.to_s.split
|
wordlist = input.to_s.split
|
||||||
words = Utils.to_integer(words)
|
words = Utils.to_integer(words)
|
||||||
|
|
||||||
l = words - 1
|
l = words - 1
|
||||||
l = 0 if l < 0
|
l = 0 if l < 0
|
||||||
|
|
||||||
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
|
wordlist.length > l ? wordlist[0..l].join(" ").concat(truncate_string.to_s) : input
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -51,8 +51,8 @@ module Liquid
|
|||||||
|
|
||||||
iteration += 1
|
iteration += 1
|
||||||
iteration = 0 if iteration >= @variables.size
|
iteration = 0 if iteration >= @variables.size
|
||||||
|
|
||||||
context.registers[:cycle][key] = iteration
|
context.registers[:cycle][key] = iteration
|
||||||
|
|
||||||
output
|
output
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -105,11 +105,9 @@ module Liquid
|
|||||||
p = Parser.new(markup)
|
p = Parser.new(markup)
|
||||||
@variable_name = p.consume(:id)
|
@variable_name = p.consume(:id)
|
||||||
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
|
raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in") unless p.id?('in')
|
||||||
|
|
||||||
collection_name = p.expression
|
collection_name = p.expression
|
||||||
@collection_name = Expression.parse(collection_name)
|
|
||||||
|
|
||||||
@name = "#{@variable_name}-#{collection_name}"
|
@name = "#{@variable_name}-#{collection_name}"
|
||||||
|
@collection_name = Expression.parse(collection_name)
|
||||||
@reversed = p.id?('reversed')
|
@reversed = p.id?('reversed')
|
||||||
|
|
||||||
while p.look(:id) && p.look(:colon, 1)
|
while p.look(:id) && p.look(:colon, 1)
|
||||||
|
|||||||
@@ -50,7 +50,19 @@ module Liquid
|
|||||||
template_name = context.evaluate(@template_name_expr)
|
template_name = context.evaluate(@template_name_expr)
|
||||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||||
|
|
||||||
partial = load_partial(template_name, context, parse_context)
|
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
|
||||||
|
|
||||||
old_template_name = context.template_name
|
old_template_name = context.template_name
|
||||||
old_partial = context.partial
|
old_partial = context.partial
|
||||||
@@ -59,15 +71,7 @@ module Liquid
|
|||||||
context.partial = true
|
context.partial = true
|
||||||
context.stack do
|
context.stack do
|
||||||
@attributes.each do |key, value|
|
@attributes.each do |key, value|
|
||||||
context[key] = evaluate(context, value)
|
context[key] = context.evaluate(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
|
end
|
||||||
|
|
||||||
if variable.is_a?(Array)
|
if variable.is_a?(Array)
|
||||||
@@ -99,24 +103,6 @@ module Liquid
|
|||||||
] + @node.attributes.values
|
] + @node.attributes.values
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
Template.register_tag('include', Include)
|
Template.register_tag('include', Include)
|
||||||
|
|||||||
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class Render < Tag
|
class Render < Tag
|
||||||
FOR = 'for'
|
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||||
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
|
||||||
|
|
||||||
disable_tags "include"
|
disable_tags "include"
|
||||||
|
|
||||||
@@ -15,13 +14,11 @@ 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)
|
||||||
with_or_for = Regexp.last_match(3)
|
variable_name = Regexp.last_match(3)
|
||||||
variable_name = Regexp.last_match(4)
|
|
||||||
|
|
||||||
@alias_name = Regexp.last_match(6)
|
@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)
|
||||||
@for = (with_or_for == FOR)
|
|
||||||
|
|
||||||
@attributes = {}
|
@attributes = {}
|
||||||
markup.scan(TagAttributes) do |key, value|
|
markup.scan(TagAttributes) do |key, value|
|
||||||
@@ -38,7 +35,11 @@ module Liquid
|
|||||||
template_name = context.evaluate(@template_name_expr)
|
template_name = context.evaluate(@template_name_expr)
|
||||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||||
|
|
||||||
partial = load_partial(template_name, context, parse_context)
|
partial = PartialCache.load(
|
||||||
|
template_name,
|
||||||
|
context: context,
|
||||||
|
parse_context: parse_context
|
||||||
|
)
|
||||||
|
|
||||||
context_variable_name = @alias_name || template_name.split('/').last
|
context_variable_name = @alias_name || template_name.split('/').last
|
||||||
|
|
||||||
@@ -47,17 +48,16 @@ module Liquid
|
|||||||
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
|
inner_context['forloop'] = forloop if forloop
|
||||||
|
|
||||||
@attributes.each do |key, value|
|
@attributes.each do |key, value|
|
||||||
inner_context[key] = evaluate(context, 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!)
|
forloop&.send(:increment!)
|
||||||
}
|
}
|
||||||
|
|
||||||
variable = @variable_name_expr ? evaluate(context, @variable_name_expr) : nil
|
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||||
if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
|
if variable.is_a?(Array)
|
||||||
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
forloop = Liquid::ForloopDrop.new(template_name, variable.count, nil)
|
||||||
variable.each { |var| render_partial_func.call(var, forloop) }
|
variable.each { |var| render_partial_func.call(var, forloop) }
|
||||||
else
|
else
|
||||||
@@ -74,20 +74,6 @@ module Liquid
|
|||||||
] + @node.attributes.values
|
] + @node.attributes.values
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
Template.register_tag('render', Render)
|
Template.register_tag('render', Render)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ module Liquid
|
|||||||
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
|
to = @attributes.key?('limit') ? from + context.evaluate(@attributes['limit']).to_i : nil
|
||||||
|
|
||||||
collection = Utils.slice_collection(collection, from, to)
|
collection = Utils.slice_collection(collection, from, to)
|
||||||
|
|
||||||
length = collection.length
|
length = collection.length
|
||||||
|
|
||||||
cols = context.evaluate(@attributes['cols']).to_i
|
cols = context.evaluate(@attributes['cols']).to_i
|
||||||
|
|||||||
@@ -252,7 +252,6 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
|
|
||||||
begin
|
begin
|
||||||
Liquid::Template.file_system = TestFileSystem.new
|
Liquid::Template.file_system = TestFileSystem.new
|
||||||
|
|
||||||
template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true)
|
template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true)
|
||||||
page = template.render('errors' => ErrorDrop.new)
|
page = template.render('errors' => ErrorDrop.new)
|
||||||
ensure
|
ensure
|
||||||
|
|||||||
@@ -214,22 +214,4 @@ class RenderTagTest < Minitest::Test
|
|||||||
assert_template_result("Product: Draft 151cm first index:1 Product: Element 155cm last index:2 ",
|
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' }])
|
"{% render 'product' for products %}", "products" => [{ 'title' => 'Draft 151cm' }, { 'title' => 'Element 155cm' }])
|
||||||
end
|
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
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user