mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 16:25:42 +03:00
Compare commits
9 Commits
render-for
...
default-fi
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
12d047aef0 | ||
|
|
c109615da7 | ||
|
|
a04c23eb38 | ||
|
|
afbe7b930f | ||
|
|
f7076e1682 | ||
|
|
135a4bc84d | ||
|
|
7ebf1aeef2 | ||
|
|
9bd5362ca1 | ||
|
|
e3fba19de0 |
@@ -7,7 +7,6 @@ module Liquid
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@blank = true
|
||||
@body = nil
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
@@ -18,7 +17,7 @@ module Liquid
|
||||
|
||||
# For backwards compatibility
|
||||
def render(context)
|
||||
@body&.render(context)
|
||||
@body.render(context)
|
||||
end
|
||||
|
||||
def blank?
|
||||
|
||||
@@ -46,7 +46,7 @@ module Liquid
|
||||
include Enumerable
|
||||
|
||||
class Timing
|
||||
attr_reader :code, :partial, :line_number, :children, :total_time, :self_time
|
||||
attr_reader :code, :partial, :line_number, :children
|
||||
|
||||
def initialize(node, partial)
|
||||
@code = node.respond_to?(:raw) ? node.raw : node
|
||||
@@ -65,17 +65,6 @@ module Liquid
|
||||
|
||||
def finish
|
||||
@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
|
||||
|
||||
def render_time
|
||||
@@ -109,8 +98,8 @@ module Liquid
|
||||
Thread.current[:liquid_profiler]
|
||||
end
|
||||
|
||||
def initialize(partial_name = "<root>")
|
||||
@partial_stack = [partial_name]
|
||||
def initialize
|
||||
@partial_stack = ["<root>"]
|
||||
|
||||
@root_timing = Timing.new("", current_partial)
|
||||
@timing_stack = [@root_timing]
|
||||
|
||||
@@ -128,13 +128,13 @@ module Liquid
|
||||
|
||||
# Join elements of the array with certain character between them
|
||||
def join(input, glue = ' ')
|
||||
InputIterator.new(input, context).join(glue)
|
||||
InputIterator.new(input).join(glue)
|
||||
end
|
||||
|
||||
# Sort elements of the array
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort(input, property = nil)
|
||||
ary = InputIterator.new(input, context)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
return [] if ary.empty?
|
||||
|
||||
@@ -154,7 +154,7 @@ module Liquid
|
||||
# Sort elements of an array ignoring case if strings
|
||||
# provide optional property with which to sort an array of hashes or drops
|
||||
def sort_natural(input, property = nil)
|
||||
ary = InputIterator.new(input, context)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
return [] if ary.empty?
|
||||
|
||||
@@ -174,7 +174,7 @@ module Liquid
|
||||
# Filter the elements of an array to those with a certain property value.
|
||||
# By default the target is any truthy value.
|
||||
def where(input, property, target_value = nil)
|
||||
ary = InputIterator.new(input, context)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
if ary.empty?
|
||||
[]
|
||||
@@ -196,7 +196,7 @@ module Liquid
|
||||
# Remove duplicate elements from an array
|
||||
# provide optional property with which to determine uniqueness
|
||||
def uniq(input, property = nil)
|
||||
ary = InputIterator.new(input, context)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
if property.nil?
|
||||
ary.uniq
|
||||
@@ -213,13 +213,13 @@ module Liquid
|
||||
|
||||
# Reverse the elements of an array
|
||||
def reverse(input)
|
||||
ary = InputIterator.new(input, context)
|
||||
ary = InputIterator.new(input)
|
||||
ary.reverse
|
||||
end
|
||||
|
||||
# map/collect on a given property
|
||||
def map(input, property)
|
||||
InputIterator.new(input, context).map do |e|
|
||||
InputIterator.new(input).map do |e|
|
||||
e = e.call if e.is_a?(Proc)
|
||||
|
||||
if property == "to_liquid"
|
||||
@@ -236,7 +236,7 @@ module Liquid
|
||||
# Remove nils within an array
|
||||
# provide optional property with which to check for nil
|
||||
def compact(input, property = nil)
|
||||
ary = InputIterator.new(input, context)
|
||||
ary = InputIterator.new(input)
|
||||
|
||||
if property.nil?
|
||||
ary.compact
|
||||
@@ -280,7 +280,7 @@ module Liquid
|
||||
unless array.respond_to?(:to_ary)
|
||||
raise ArgumentError, "concat filter requires an array argument"
|
||||
end
|
||||
InputIterator.new(input, context).concat(array)
|
||||
InputIterator.new(input).concat(array)
|
||||
end
|
||||
|
||||
# prepend a string to another
|
||||
@@ -439,8 +439,6 @@ module Liquid
|
||||
|
||||
private
|
||||
|
||||
attr_reader :context
|
||||
|
||||
def raise_property_error(property)
|
||||
raise Liquid::ArgumentError, "cannot select the property '#{property}'"
|
||||
end
|
||||
@@ -469,8 +467,7 @@ module Liquid
|
||||
class InputIterator
|
||||
include Enumerable
|
||||
|
||||
def initialize(input, context)
|
||||
@context = context
|
||||
def initialize(input)
|
||||
@input = if input.is_a?(Array)
|
||||
input.flatten
|
||||
elsif input.is_a?(Hash)
|
||||
@@ -509,7 +506,6 @@ module Liquid
|
||||
|
||||
def each
|
||||
@input.each do |e|
|
||||
e.context = @context if e.respond_to?(:context=)
|
||||
yield(e.respond_to?(:to_liquid) ? e.to_liquid : e)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
module Liquid
|
||||
class Tag
|
||||
attr_reader :nodelist, :tag_name, :line_number, :parse_context, :markup
|
||||
attr_reader :nodelist, :tag_name, :line_number, :parse_context
|
||||
alias_method :options, :parse_context
|
||||
include ParserSwitching
|
||||
|
||||
@@ -17,6 +17,8 @@ module Liquid
|
||||
disabled_tags.push(*tags)
|
||||
end
|
||||
|
||||
private :new
|
||||
|
||||
def disabled_tags
|
||||
@disabled_tags ||= []
|
||||
end
|
||||
|
||||
@@ -18,7 +18,8 @@ module Liquid
|
||||
|
||||
attr_reader :to, :from
|
||||
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ Syntax
|
||||
@to = Regexp.last_match(1)
|
||||
@from = Variable.new(Regexp.last_match(2), options)
|
||||
|
||||
@@ -15,13 +15,13 @@ module Liquid
|
||||
class Capture < Block
|
||||
Syntax = /(#{VariableSignature}+)/o
|
||||
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ Syntax
|
||||
@to = Regexp.last_match(1)
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.capture")
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
def render_to_output_buffer(context, output)
|
||||
|
||||
@@ -7,7 +7,8 @@ module Liquid
|
||||
|
||||
attr_reader :blocks, :left
|
||||
|
||||
def parse(tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@blocks = []
|
||||
|
||||
if markup =~ Syntax
|
||||
@@ -15,7 +16,9 @@ module Liquid
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.case")
|
||||
end
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
body = BlockBody.new
|
||||
body = @blocks.last.attachment while parse_body(body, tokens)
|
||||
end
|
||||
|
||||
@@ -19,7 +19,8 @@ module Liquid
|
||||
|
||||
attr_reader :variables
|
||||
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
case markup
|
||||
when NamedSyntax
|
||||
@variables = variables_from_string(Regexp.last_match(2))
|
||||
|
||||
@@ -20,7 +20,8 @@ module Liquid
|
||||
# Hello: -3
|
||||
#
|
||||
class Decrement < Tag
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@variable = markup.strip
|
||||
end
|
||||
|
||||
|
||||
@@ -12,14 +12,13 @@ module Liquid
|
||||
# {% echo user | link %}
|
||||
#
|
||||
class Echo < Tag
|
||||
attr_reader :variable
|
||||
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, parse_context)
|
||||
super
|
||||
@variable = Variable.new(markup, parse_context)
|
||||
end
|
||||
|
||||
def render(context)
|
||||
variable&.render_to_output_buffer(context, +'')
|
||||
@variable.render_to_output_buffer(context, +'')
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -50,11 +50,15 @@ module Liquid
|
||||
|
||||
attr_reader :collection_name, :variable_name, :limit, :from
|
||||
|
||||
def parse(tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@from = @limit = nil
|
||||
parse_with_selected_parser(markup)
|
||||
@for_block = BlockBody.new
|
||||
@else_block = nil
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
return unless parse_body(@for_block, tokens)
|
||||
parse_body(@else_block, tokens)
|
||||
end
|
||||
|
||||
@@ -18,13 +18,17 @@ module Liquid
|
||||
|
||||
attr_reader :blocks
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@blocks = []
|
||||
push_block('if', markup)
|
||||
end
|
||||
|
||||
def nodelist
|
||||
@blocks.map(&:attachment)
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
@blocks = []
|
||||
push_block('if', markup)
|
||||
while parse_body(@blocks.last.attachment, tokens)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -16,18 +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 parse(_tokens)
|
||||
if markup =~ SYNTAX
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
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 = {}
|
||||
@@ -41,6 +41,9 @@ module Liquid
|
||||
end
|
||||
end
|
||||
|
||||
def parse(_tokens)
|
||||
end
|
||||
|
||||
def render_to_output_buffer(context, output)
|
||||
template_name = context.evaluate(@template_name_expr)
|
||||
raise ArgumentError, options[:locale].t("errors.argument.include") unless template_name
|
||||
@@ -51,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)
|
||||
|
||||
@@ -17,7 +17,8 @@ module Liquid
|
||||
# Hello: 2
|
||||
#
|
||||
class Increment < Tag
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
@variable = markup.strip
|
||||
end
|
||||
|
||||
|
||||
@@ -5,8 +5,13 @@ module Liquid
|
||||
Syntax = /\A\s*\z/
|
||||
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om
|
||||
|
||||
def parse(tokens)
|
||||
def initialize(tag_name, markup, parse_context)
|
||||
super
|
||||
|
||||
ensure_valid_markup(tag_name, markup, parse_context)
|
||||
end
|
||||
|
||||
def parse(tokens)
|
||||
@body = +''
|
||||
while (token = tokens.shift)
|
||||
if token =~ FullTokenPossiblyInvalid
|
||||
|
||||
@@ -2,20 +2,19 @@
|
||||
|
||||
module Liquid
|
||||
class Render < Tag
|
||||
SYNTAX = /(#{QuotedString}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?(\s+(?:as)\s+(#{VariableSegment}+))?/o
|
||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||
|
||||
disable_tags "include"
|
||||
|
||||
attr_reader :template_name_expr, :attributes
|
||||
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
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 = {}
|
||||
@@ -39,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
|
||||
|
||||
@@ -6,7 +6,8 @@ module Liquid
|
||||
|
||||
attr_reader :variable_name, :collection_name, :attributes
|
||||
|
||||
def parse(_tokens)
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
if markup =~ Syntax
|
||||
@variable_name = Regexp.last_match(1)
|
||||
@collection_name = Expression.parse(Regexp.last_match(2))
|
||||
@@ -17,7 +18,6 @@ module Liquid
|
||||
else
|
||||
raise SyntaxError, options[:locale].t("errors.syntax.table_row")
|
||||
end
|
||||
super
|
||||
end
|
||||
|
||||
def render_to_output_buffer(context, output)
|
||||
|
||||
@@ -254,7 +254,7 @@ module Liquid
|
||||
if @profiling && !context.partial
|
||||
raise "Profiler not loaded, require 'liquid/profiler' first" unless defined?(Liquid::Profiler)
|
||||
|
||||
@profiler = Profiler.new(context.template_name)
|
||||
@profiler = Profiler.new
|
||||
@profiler.start
|
||||
|
||||
begin
|
||||
|
||||
@@ -179,11 +179,6 @@ class DropsTest < Minitest::Test
|
||||
assert_equal(' carrot ', output)
|
||||
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
|
||||
output = Liquid::Template.parse(' {{ product.context.foo }} ').render!('product' => ProductDrop.new, 'foo' => "monkey")
|
||||
assert_equal(' monkey ', output)
|
||||
|
||||
@@ -153,19 +153,4 @@ class RenderProfilingTest < Minitest::Test
|
||||
# Will profile each invocation of the for block
|
||||
assert_equal(2, t.profiler[0].children.length)
|
||||
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
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'test_helper'
|
||||
|
||||
class TagTest < Minitest::Test
|
||||
include Liquid
|
||||
|
||||
def test_all_tags_with_no_parse_can_render
|
||||
Template.tags.each do |key, _tag|
|
||||
Template.tags[key].new(key, '', ParseContext.new).render(Context.new)
|
||||
assert_nil(nil)
|
||||
end
|
||||
end
|
||||
|
||||
def test_all_tags_are_registered
|
||||
tags = Template.tags.map { |key, _tag| key }
|
||||
expected_tags = %w(assign break capture case comment continue cycle decrement echo for if ifchanged include increment raw render tablerow unless)
|
||||
assert_equal(expected_tags, tags.sort)
|
||||
end
|
||||
end
|
||||
@@ -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