mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-20 11:20:41 -07:00
Liquid::TablerowloopDrop
This commit is contained in:
@@ -48,6 +48,7 @@ require 'liquid/lexer'
|
|||||||
require 'liquid/parser'
|
require 'liquid/parser'
|
||||||
require 'liquid/i18n'
|
require 'liquid/i18n'
|
||||||
require 'liquid/drop'
|
require 'liquid/drop'
|
||||||
|
require 'liquid/tablerowloop_drop'
|
||||||
require 'liquid/forloop_drop'
|
require 'liquid/forloop_drop'
|
||||||
require 'liquid/extensions'
|
require 'liquid/extensions'
|
||||||
require 'liquid/errors'
|
require 'liquid/errors'
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
module Liquid
|
||||||
|
class TablerowloopDrop < Drop
|
||||||
|
def initialize(length, cols)
|
||||||
|
@length = length
|
||||||
|
@row = 1
|
||||||
|
@col = 1
|
||||||
|
@cols = cols
|
||||||
|
@index = 0
|
||||||
|
end
|
||||||
|
|
||||||
|
attr_reader :length, :col, :row
|
||||||
|
|
||||||
|
def index
|
||||||
|
@index + 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def index0
|
||||||
|
@index
|
||||||
|
end
|
||||||
|
|
||||||
|
def col0
|
||||||
|
@col - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def rindex
|
||||||
|
@length - @index
|
||||||
|
end
|
||||||
|
|
||||||
|
def rindex0
|
||||||
|
@length - @index - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def first
|
||||||
|
@index == 0
|
||||||
|
end
|
||||||
|
|
||||||
|
def last
|
||||||
|
@index == @length - 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def col_first
|
||||||
|
@col == 1
|
||||||
|
end
|
||||||
|
|
||||||
|
def col_last
|
||||||
|
@col == @cols
|
||||||
|
end
|
||||||
|
|
||||||
|
protected
|
||||||
|
|
||||||
|
def increment!
|
||||||
|
@index += 1
|
||||||
|
|
||||||
|
if @col == @cols
|
||||||
|
@col = 1
|
||||||
|
@row += 1
|
||||||
|
else
|
||||||
|
@col += 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -28,36 +28,21 @@ module Liquid
|
|||||||
|
|
||||||
cols = context.evaluate(@attributes['cols'.freeze]).to_i
|
cols = context.evaluate(@attributes['cols'.freeze]).to_i
|
||||||
|
|
||||||
row = 1
|
|
||||||
col = 0
|
|
||||||
|
|
||||||
result = "<tr class=\"row1\">\n"
|
result = "<tr class=\"row1\">\n"
|
||||||
context.stack do
|
context.stack do
|
||||||
|
tablerowloop = Liquid::TablerowloopDrop.new(length, cols)
|
||||||
|
context['tablerowloop'.freeze] = tablerowloop
|
||||||
|
|
||||||
collection.each_with_index do |item, index|
|
collection.each_with_index do |item, index|
|
||||||
context[@variable_name] = item
|
context[@variable_name] = item
|
||||||
context['tablerowloop'.freeze] = {
|
|
||||||
'length'.freeze => length,
|
|
||||||
'index'.freeze => index + 1,
|
|
||||||
'index0'.freeze => index,
|
|
||||||
'col'.freeze => col + 1,
|
|
||||||
'col0'.freeze => col,
|
|
||||||
'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#{tablerowloop.col}\">" << super << '</td>'
|
||||||
|
|
||||||
result << "<td class=\"col#{col}\">" << super << '</td>'
|
if tablerowloop.col_last && !tablerowloop.last
|
||||||
|
result << "</tr>\n<tr class=\"row#{tablerowloop.row + 1}\">"
|
||||||
if col == cols && (index != length - 1)
|
|
||||||
col = 0
|
|
||||||
row += 1
|
|
||||||
result << "</tr>\n<tr class=\"row#{row}\">"
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
tablerowloop.send(:increment!)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
result << "</tr>\n"
|
result << "</tr>\n"
|
||||||
|
|||||||
Reference in New Issue
Block a user