class Liquid::TableRow
@liquid_optional_param range [untyped] A custom numeric range to iterate over.
@liquid_optional_param offset [number] The 1-based index to start iterating at.
@liquid_optional_param limit [number] The number of iterations to perform.
@liquid_optional_param cols [number] The number of columns that the table should have.
@liquid_syntax_keyword expression The expression to render.
@liquid_syntax_keyword array The array to iterate over.
@liquid_syntax_keyword variable The current item in the array.
{% endtablerow %}
expression
{% tablerow variable in array %}
@liquid_syntax
> Every ‘tablerow` loop has an associated [`tablerowloop` object](/docs/api/liquid/objects/tablerowloop) with information about the loop.
> Tip:
The `tablerow` tag must be wrapped in HTML `<table>` and `</table>` tags.
@liquid_description
Generates HTML table rows for every item in an array.
@liquid_summary
@liquid_name tablerow
@liquid_category iteration
@liquid_type tag
@liquid_public_docs
def initialize(tag_name, markup, options)
def initialize(tag_name, markup, options) super 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
def render_to_output_buffer(context, output)
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>' 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
def to_integer(value)
def to_integer(value) value.to_i rescue NoMethodError raise Liquid::ArgumentError, "invalid integer" end