Sha256: d25d0e1e80893da83eb554ae59c73d53a168351263a7258c9c84c288a1fea0f0

Contents?: true

Size: 1.9 KB

Versions: 20

Compression:

Stored size: 1.9 KB

Contents

module Liquid
  class TableRow < Block                                             
    Syntax = /(\w+)\s+in\s+(#{AllowedVariableCharacters}+)/   
    
    def initialize(markup, tokens)
      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("Syntax Error in 'table_row loop' - Valid syntax: table_row [item] in [collection] cols=3")
      end
    end
    
    def render(context)        
      collection = context[@collection_name] or return ''
      
      if @attributes['limit'] or @attributes['offset']
        limit = context[@attributes['limit']] || -1
        offset = context[@attributes['offset']] || 0
        collection = collection[offset.to_i..(limit.to_i + offset.to_i - 1)]
      end
            
      length = collection.length
      
      cols = context[@attributes['cols']].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'] = {
            'length'  => length,
            'index'   => index + 1, 
            'index0'  => index, 
            'rindex'  => length - index,
            'rindex0' => length - index -1,
            'first'   => (index == 0),
            'last'    => (index == length - 1) }
                                
          result << ["<td class=\"col#{col += 1}\">"] + render_all(@nodelist, context) + ['</td>']

          if col == cols and not (index == length - 1)            
            col  = 0
            result << ["</tr>\n<tr class=\"row#{row += 1}\">"] 
          end
          
        end
      end
      result + ["</tr>\n"]
    end    
  end
  
  Template.register_tag('tablerow', TableRow)  
end

Version data entries

20 entries across 20 versions & 7 rubygems

Version Path
aslakjo-aslakjo-comatose-2.0.5.1 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.2 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.3 lib/liquid/htmltags.rb
darthapo-comatose-2.0.3 lib/liquid/htmltags.rb
darthapo-comatose-2.0.4 lib/liquid/htmltags.rb
darthapo-comatose-2.0.5 lib/liquid/htmltags.rb
darthapo-comatose-2.0 lib/liquid/htmltags.rb
jcnetdev-comatose-2.0.1 lib/liquid/htmltags.rb
staugaard-comatose-2.0.2 lib/liquid/htmltags.rb
staugaard-comatose-2.0.3 lib/liquid/htmltags.rb
comatose-2.0.5 lib/liquid/htmltags.rb
comatose-2.0.1 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.12 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.11 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.10 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.9 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.8 lib/liquid/htmltags.rb
aslakjo-comatose-2.0.5.7 lib/liquid/htmltags.rb
merb_comatose-0.0.3 lib/liquid/htmltags.rb
merb_comatose-0.0.2 lib/liquid/htmltags.rb