mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 08:15:41 +03:00
Compare commits
5 Commits
render-for
...
experiment
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3f439f73ba | ||
|
|
5face68cc8 | ||
|
|
57c9cf64eb | ||
|
|
e83b1e4159 | ||
|
|
3784020a8d |
@@ -892,7 +892,7 @@ Lint/FormatParameterMismatch:
|
|||||||
Enabled: true
|
Enabled: true
|
||||||
|
|
||||||
Lint/HandleExceptions:
|
Lint/HandleExceptions:
|
||||||
Enabled: true
|
AllowComments: 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
|
||||||
|
|||||||
@@ -1,11 +1,15 @@
|
|||||||
language: ruby
|
language: ruby
|
||||||
cache: bundler
|
cache: bundler
|
||||||
|
os: [osx, linux]
|
||||||
|
|
||||||
rvm:
|
rvm:
|
||||||
- 2.4
|
- 2.4
|
||||||
- 2.5
|
- 2.5
|
||||||
- &latest_ruby 2.6
|
- &latest_ruby 2.6
|
||||||
- 2.7
|
- 2.7
|
||||||
|
- 2.7.0-preview1
|
||||||
|
- 2.7.0-preview2
|
||||||
|
- 2.7.0-preview3
|
||||||
- ruby-head
|
- ruby-head
|
||||||
|
|
||||||
matrix:
|
matrix:
|
||||||
@@ -15,6 +19,9 @@ matrix:
|
|||||||
name: Profiling Memory Usage
|
name: Profiling Memory Usage
|
||||||
allow_failures:
|
allow_failures:
|
||||||
- rvm: ruby-head
|
- rvm: ruby-head
|
||||||
|
- rvm: 2.7
|
||||||
|
- rvm: 2.7.0-preview2
|
||||||
|
- rvm: 2.7.0-preview3
|
||||||
|
|
||||||
branches:
|
branches:
|
||||||
only:
|
only:
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ 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,8 +7,10 @@ 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
|
||||||
@@ -20,6 +22,7 @@ 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,6 +9,7 @@ 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,9 +78,12 @@ 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
|
||||||
|
|
||||||
@@ -88,8 +91,10 @@ 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,9 +105,11 @@ 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
|
||||||
@name = "#{@variable_name}-#{collection_name}"
|
|
||||||
@collection_name = Expression.parse(collection_name)
|
@collection_name = Expression.parse(collection_name)
|
||||||
|
|
||||||
|
@name = "#{@variable_name}-#{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)
|
||||||
|
|||||||
@@ -2,7 +2,8 @@
|
|||||||
|
|
||||||
module Liquid
|
module Liquid
|
||||||
class Render < Tag
|
class Render < Tag
|
||||||
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
FOR = 'for'
|
||||||
|
SYNTAX = /(#{QuotedString}+)(\s+(with|#{FOR})\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||||
|
|
||||||
disable_tags "include"
|
disable_tags "include"
|
||||||
|
|
||||||
@@ -14,11 +15,13 @@ 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)
|
with_or_for = Regexp.last_match(3)
|
||||||
|
variable_name = Regexp.last_match(4)
|
||||||
|
|
||||||
@alias_name = Regexp.last_match(5)
|
@alias_name = Regexp.last_match(6)
|
||||||
@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|
|
||||||
@@ -48,6 +51,7 @@ 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] = context.evaluate(value)
|
inner_context[key] = context.evaluate(value)
|
||||||
end
|
end
|
||||||
@@ -57,7 +61,7 @@ module Liquid
|
|||||||
}
|
}
|
||||||
|
|
||||||
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
|
||||||
if variable.is_a?(Array)
|
if @for && variable.respond_to?(:each) && variable.respond_to?(:count)
|
||||||
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
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ 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,6 +252,7 @@ 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,4 +214,22 @@ 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