Class: Liquid::For

Inherits:
Block show all
Defined in:
lib/liquid/tags/for.rb

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

Syntax =
/\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o

Constants inherited from Block

Block::MAX_DEPTH

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #parse_context, #tag_name

Instance Method Summary collapse

Methods inherited from Block

#blank?, #block_delimiter, #block_name, #raise_tag_never_closed, raise_unknown_tag, #render

Methods inherited from Tag

#blank?, disable_tags, #name, parse, #raw, #render

Methods included from ParserSwitching

#parse_with_selected_parser, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(tag_name, markup, options) ⇒ For

Returns a new instance of For.



32
33
34
35
36
37
38
# File 'lib/liquid/tags/for.rb', line 32

def initialize(tag_name, markup, options)
  super
  @from = @limit = nil
  parse_with_selected_parser(markup)
  @for_block = new_body
  @else_block = nil
end

Instance Attribute Details

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



30
31
32
# File 'lib/liquid/tags/for.rb', line 30

def collection_name
  @collection_name
end

#fromObject (readonly)

Returns the value of attribute from.



30
31
32
# File 'lib/liquid/tags/for.rb', line 30

def from
  @from
end

#limitObject (readonly)

Returns the value of attribute limit.



30
31
32
# File 'lib/liquid/tags/for.rb', line 30

def limit
  @limit
end

#variable_nameObject (readonly)

Returns the value of attribute variable_name.



30
31
32
# File 'lib/liquid/tags/for.rb', line 30

def variable_name
  @variable_name
end

Instance Method Details

#nodelistObject



52
53
54
# File 'lib/liquid/tags/for.rb', line 52

def nodelist
  @else_block ? [@for_block, @else_block] : [@for_block]
end

#parse(tokens) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/liquid/tags/for.rb', line 40

def parse(tokens)
  if parse_body(@for_block, tokens)
    parse_body(@else_block, tokens)
  end
  if blank?
    @else_block&.remove_blank_strings
    @for_block.remove_blank_strings
  end
  @else_block&.freeze
  @for_block.freeze
end

#render_to_output_buffer(context, output) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/liquid/tags/for.rb', line 61

def render_to_output_buffer(context, output)
  segment = collection_segment(context)

  if segment.empty?
    render_else(context, output)
  else
    render_segment(context, output, segment)
  end

  output
end

#unknown_tag(tag, markup, tokens) ⇒ Object



56
57
58
59
# File 'lib/liquid/tags/for.rb', line 56

def unknown_tag(tag, markup, tokens)
  return super unless tag == 'else'
  @else_block = new_body
end