Class: Liquid::TableRow

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

Constant Summary collapse

Syntax =
/(\w+)\s+in\s+(#{QuotedFragment}+)/o

Constants inherited from Block

Block::MAX_DEPTH

Instance Attribute Summary

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, #unknown_tag

Methods inherited from Tag

#blank?, #name, #parse, parse, #raw

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(tag_name, markup, options) ⇒ TableRow

Returns a new instance of TableRow.



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/liquid/tags/table_row.rb', line 5

def initialize(tag_name, markup, options)
  super
  if markup =~ Syntax
    @variable_name = $1
    @collection_name = Expression.parse($2)
    @attributes = {}
    markup.scan(TagAttributes) do |key, value|
      @attributes[key] = Expression.parse(value)
    end
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.table_row".freeze))
  end
end

Instance Method Details

#render(context) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/liquid/tags/table_row.rb', line 19

def render(context)
  collection = context.evaluate(@collection_name) or return ''.freeze

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

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

  length = collection.length

  cols = context.evaluate(@attributes['cols'.freeze]).to_i

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

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

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

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

      tablerowloop.send(:increment!)
    end
  end
  result << "</tr>\n"
  result
end