Compare commits

..

2 Commits

Author SHA1 Message Date
Mike Angell
56a1034ac2 Remove forloop guard 2019-10-15 20:37:12 +10:00
Mike Angell
1c3dcb0ddc Add forloop to render for syntax 2019-10-14 17:24:34 +10:00
17 changed files with 67 additions and 44 deletions

View File

@@ -7,7 +7,6 @@ module Liquid
def initialize(tag_name, markup, options) def initialize(tag_name, markup, options)
super super
@blank = true @blank = true
@body = nil
end end
def parse(tokens) def parse(tokens)
@@ -18,7 +17,7 @@ module Liquid
# For backwards compatibility # For backwards compatibility
def render(context) def render(context)
@body&.render(context) @body.render(context)
end end
def blank? def blank?

View File

@@ -2,7 +2,7 @@
module Liquid module Liquid
class Tag 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 alias_method :options, :parse_context
include ParserSwitching include ParserSwitching
@@ -17,6 +17,8 @@ module Liquid
disabled_tags.push(*tags) disabled_tags.push(*tags)
end end
private :new
def disabled_tags def disabled_tags
@disabled_tags ||= [] @disabled_tags ||= []
end end

View File

@@ -18,7 +18,8 @@ module Liquid
attr_reader :to, :from attr_reader :to, :from
def parse(_tokens) def initialize(tag_name, markup, options)
super
if markup =~ Syntax if markup =~ Syntax
@to = Regexp.last_match(1) @to = Regexp.last_match(1)
@from = Variable.new(Regexp.last_match(2), options) @from = Variable.new(Regexp.last_match(2), options)

View File

@@ -15,13 +15,13 @@ module Liquid
class Capture < Block class Capture < Block
Syntax = /(#{VariableSignature}+)/o Syntax = /(#{VariableSignature}+)/o
def parse(_tokens) def initialize(tag_name, markup, options)
super
if markup =~ Syntax if markup =~ Syntax
@to = Regexp.last_match(1) @to = Regexp.last_match(1)
else else
raise SyntaxError, options[:locale].t("errors.syntax.capture") raise SyntaxError, options[:locale].t("errors.syntax.capture")
end end
super
end end
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)

View File

@@ -7,7 +7,8 @@ module Liquid
attr_reader :blocks, :left attr_reader :blocks, :left
def parse(tokens) def initialize(tag_name, markup, options)
super
@blocks = [] @blocks = []
if markup =~ Syntax if markup =~ Syntax
@@ -15,7 +16,9 @@ module Liquid
else else
raise SyntaxError, options[:locale].t("errors.syntax.case") raise SyntaxError, options[:locale].t("errors.syntax.case")
end end
end
def parse(tokens)
body = BlockBody.new body = BlockBody.new
body = @blocks.last.attachment while parse_body(body, tokens) body = @blocks.last.attachment while parse_body(body, tokens)
end end

View File

@@ -19,7 +19,8 @@ module Liquid
attr_reader :variables attr_reader :variables
def parse(_tokens) def initialize(tag_name, markup, options)
super
case markup case markup
when NamedSyntax when NamedSyntax
@variables = variables_from_string(Regexp.last_match(2)) @variables = variables_from_string(Regexp.last_match(2))

View File

@@ -20,7 +20,8 @@ module Liquid
# Hello: -3 # Hello: -3
# #
class Decrement < Tag class Decrement < Tag
def parse(_tokens) def initialize(tag_name, markup, options)
super
@variable = markup.strip @variable = markup.strip
end end

View File

@@ -12,14 +12,13 @@ module Liquid
# {% echo user | link %} # {% echo user | link %}
# #
class Echo < Tag class Echo < Tag
attr_reader :variable def initialize(tag_name, markup, parse_context)
super
def parse(_tokens)
@variable = Variable.new(markup, parse_context) @variable = Variable.new(markup, parse_context)
end end
def render(context) def render(context)
variable&.render_to_output_buffer(context, +'') @variable.render_to_output_buffer(context, +'')
end end
end end

View File

@@ -50,11 +50,15 @@ module Liquid
attr_reader :collection_name, :variable_name, :limit, :from attr_reader :collection_name, :variable_name, :limit, :from
def parse(tokens) def initialize(tag_name, markup, options)
super
@from = @limit = nil @from = @limit = nil
parse_with_selected_parser(markup) parse_with_selected_parser(markup)
@for_block = BlockBody.new @for_block = BlockBody.new
@else_block = nil @else_block = nil
end
def parse(tokens)
return unless parse_body(@for_block, tokens) return unless parse_body(@for_block, tokens)
parse_body(@else_block, tokens) parse_body(@else_block, tokens)
end end

View File

@@ -18,13 +18,17 @@ module Liquid
attr_reader :blocks attr_reader :blocks
def initialize(tag_name, markup, options)
super
@blocks = []
push_block('if', markup)
end
def nodelist def nodelist
@blocks.map(&:attachment) @blocks.map(&:attachment)
end end
def parse(tokens) def parse(tokens)
@blocks = []
push_block('if', markup)
while parse_body(@blocks.last.attachment, tokens) while parse_body(@blocks.last.attachment, tokens)
end end
end end

View File

@@ -21,7 +21,9 @@ module Liquid
attr_reader :template_name_expr, :variable_name_expr, :attributes attr_reader :template_name_expr, :variable_name_expr, :attributes
def parse(_tokens) def initialize(tag_name, markup, options)
super
if markup =~ SYNTAX if markup =~ SYNTAX
template_name = Regexp.last_match(1) template_name = Regexp.last_match(1)
@@ -41,6 +43,9 @@ module Liquid
end end
end end
def parse(_tokens)
end
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)
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

View File

@@ -17,7 +17,8 @@ module Liquid
# Hello: 2 # Hello: 2
# #
class Increment < Tag class Increment < Tag
def parse(_tokens) def initialize(tag_name, markup, options)
super
@variable = markup.strip @variable = markup.strip
end end

View File

@@ -5,8 +5,13 @@ module Liquid
Syntax = /\A\s*\z/ Syntax = /\A\s*\z/
FullTokenPossiblyInvalid = /\A(.*)#{TagStart}\s*(\w+)\s*(.*)?#{TagEnd}\z/om 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) ensure_valid_markup(tag_name, markup, parse_context)
end
def parse(tokens)
@body = +'' @body = +''
while (token = tokens.shift) while (token = tokens.shift)
if token =~ FullTokenPossiblyInvalid if token =~ FullTokenPossiblyInvalid

View File

@@ -8,7 +8,9 @@ module Liquid
attr_reader :template_name_expr, :attributes 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 raise SyntaxError, options[:locale].t("errors.syntax.render") unless markup =~ SYNTAX
template_name = Regexp.last_match(1) template_name = Regexp.last_match(1)
@@ -41,19 +43,26 @@ module Liquid
context_variable_name = @alias_name || template_name.split('/').last context_variable_name = @alias_name || template_name.split('/').last
render_partial_func = ->(var) { 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? 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 variable = @variable_name_expr ? context.evaluate(@variable_name_expr) : nil
variable.is_a?(Array) ? variable.each(&render_partial_func) : render_partial_func.call(variable) 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

View File

@@ -6,7 +6,8 @@ module Liquid
attr_reader :variable_name, :collection_name, :attributes attr_reader :variable_name, :collection_name, :attributes
def parse(_tokens) def initialize(tag_name, markup, options)
super
if markup =~ Syntax if markup =~ Syntax
@variable_name = Regexp.last_match(1) @variable_name = Regexp.last_match(1)
@collection_name = Expression.parse(Regexp.last_match(2)) @collection_name = Expression.parse(Regexp.last_match(2))
@@ -17,7 +18,6 @@ module Liquid
else else
raise SyntaxError, options[:locale].t("errors.syntax.table_row") raise SyntaxError, options[:locale].t("errors.syntax.table_row")
end end
super
end end
def render_to_output_buffer(context, output) def render_to_output_buffer(context, output)

View File

@@ -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

View File

@@ -205,4 +205,13 @@ class RenderTagTest < Minitest::Test
assert_template_result("Product: Draft 151cm Product: Element 155cm ", assert_template_result("Product: Draft 151cm Product: Element 155cm ",
"{% 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_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