Class: Liquid::TableRow

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

Constant Summary

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

Constants inherited from Block

Block::ContentOfVariable, Block::FullToken, Block::IsTag, Block::IsVariable

Instance Attribute Summary

Attributes inherited from Tag

#nodelist, #options, #warnings

Instance Method Summary (collapse)

Methods inherited from Block

#blank?, #block_delimiter, #block_name, #create_variable, #end_tag, #parse, #unknown_tag, #warnings

Methods inherited from Tag

#blank?, #name, parse, #parse, #parse_with_selected_parser

Constructor Details

- (TableRow) initialize(tag_name, markup, options)



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 = $2
    @attributes = {}
    markup.scan(TagAttributes) do |key, value|
      @attributes[key] = value
    end
  else
    raise SyntaxError.new(options[:locale].t("errors.syntax.table_row".freeze))
  end
end

Instance Method Details

- (Object) render(context)



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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/liquid/tags/table_row.rb', line 19

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

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

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

  length = collection.length

  cols = context[@attributes['cols'.freeze]].to_i

  row = 1
  col = 0

  result = "<tr class=\"row1\">\n"
  context.stack do

    collection.each_with_index do |item, index|
      context[@variable_name] = item
      context['tablerowloop'.freeze] = {
        'length'.freeze    => length,
        'index'.freeze     => index + 1,
        'index0'.freeze    => index,
        'col'.freeze       => col + 1,
        'col0'.freeze      => col,
        'index0'.freeze    => index,
        'rindex'.freeze    => length - index,
        'rindex0'.freeze   => length - index - 1,
        'first'.freeze     => (index == 0),
        'last'.freeze      => (index == length - 1),
        'col_first'.freeze => (col == 0),
        'col_last'.freeze  => (col == cols - 1)
      }


      col += 1

      result << "<td class=\"col#{col}\">" << render_all(@nodelist, context) << '</td>'

      if col == cols and (index != length - 1)
        col  = 0
        row += 1
        result << "</tr>\n<tr class=\"row#{row}\">"
      end

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