Class: Liquid::TableRow

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

Defined Under Namespace

Classes: ParseTreeVisitor

Constant Summary collapse

Syntax =
/(\w+)\s+in\s+(#{QuotedFragment}+)/o
ALLOWED_ATTRIBUTES =
['cols', 'limit', 'offset', 'range'].freeze

Constants inherited from Block

Block::MAX_DEPTH

Instance Attribute Summary collapse

Attributes inherited from Tag

#line_number, #nodelist, #parse_context, #tag_name

Instance Method Summary collapse

Methods inherited from Block

#blank?, #block_delimiter, #block_name, #nodelist, #parse, #raise_tag_never_closed, raise_unknown_tag, #render, #unknown_tag

Methods inherited from Tag

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

Methods included from ParserSwitching

#parse_with_selected_parser, #strict2_mode?, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(tag_name, markup, options) ⇒ TableRow

Returns a new instance of TableRow.



32
33
34
35
# File 'lib/liquid/tags/table_row.rb', line 32

def initialize(tag_name, markup, options)
  super
  parse_with_selected_parser(markup)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#collection_nameObject (readonly)

Returns the value of attribute collection_name.



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

def collection_name
  @collection_name
end

#variable_nameObject (readonly)

Returns the value of attribute variable_name.



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

def variable_name
  @variable_name
end

Instance Method Details

#lax_parse(markup) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/liquid/tags/table_row.rb', line 69

def lax_parse(markup)
  if markup =~ Syntax
    @variable_name   = Regexp.last_match(1)
    @collection_name = parse_expression(Regexp.last_match(2))
    @attributes      = {}
    markup.scan(TagAttributes) do |key, value|
      @attributes[key] = parse_expression(value)
    end
  else
    raise SyntaxError, options[:locale].t("errors.syntax.table_row")
  end
end

#render_to_output_buffer(context, output) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/liquid/tags/table_row.rb', line 82

def render_to_output_buffer(context, output)
  (collection = context.evaluate(@collection_name)) || (return '')

  from = @attributes.key?('offset') ? to_integer(context.evaluate(@attributes['offset'])) : 0
  to = @attributes.key?('limit') ? from + to_integer(context.evaluate(@attributes['limit'])) : nil

  collection = Utils.slice_collection(collection, from, to)
  length     = collection.length

  cols = @attributes.key?('cols') ? to_integer(context.evaluate(@attributes['cols'])) : length

  output << "<tr class=\"row1\">\n"
  context.stack do
    tablerowloop = Liquid::TablerowloopDrop.new(length, cols)
    context['tablerowloop'] = tablerowloop

    collection.each do |item|
      context[@variable_name] = item

      output << "<td class=\"col#{tablerowloop.col}\">"
      super
      output << '</td>'

      # Handle any interrupts if they exist.
      if context.interrupt?
        interrupt = context.pop_interrupt
        break if interrupt.is_a?(BreakInterrupt)
      end

      if tablerowloop.col_last && !tablerowloop.last
        output << "</tr>\n<tr class=\"row#{tablerowloop.row + 1}\">"
      end

      tablerowloop.send(:increment!)
    end
  end

  output << "</tr>\n"
  output
end

#strict2_parse(markup) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/liquid/tags/table_row.rb', line 37

def strict2_parse(markup)
  p = @parse_context.new_parser(markup)

  @variable_name = p.consume(:id)

  unless p.id?("in")
    raise SyntaxError, options[:locale].t("errors.syntax.for_invalid_in")
  end

  @collection_name = safe_parse_expression(p)

  p.consume?(:comma)

  @attributes = {}
  while p.look(:id)
    key = p.consume
    unless ALLOWED_ATTRIBUTES.include?(key)
      raise SyntaxError, options[:locale].t("errors.syntax.table_row_invalid_attribute", attribute: key)
    end

    p.consume(:colon)
    @attributes[key] = safe_parse_expression(p)
    p.consume?(:comma)
  end

  p.consume(:end_of_string)
end

#strict_parse(markup) ⇒ Object



65
66
67
# File 'lib/liquid/tags/table_row.rb', line 65

def strict_parse(markup)
  lax_parse(markup)
end