Class: Liquid::For
Overview
“For” iterates over an array or collection. Several useful variables are available to you within the loop.
Basic usage:
{% for item in collection %}
{{ forloop.index }}: {{ item.name }}
{% endfor %}
Advanced usage:
{% for item in collection %}
<div {% if forloop.first %}class="first"{% endif %}>
Item {{ forloop.index }}: {{ item.name }}
</div>
{% else %}
There is nothing in the collection.
{% endfor %}
You can also define a limit and offset much like SQL. Remember that offset starts at 0 for the first item.
{% for item in collection limit:5 offset:10 %}
{{ item.name }}
{% end %}
To reverse the for loop simply use {% for item in collection reversed %} (note that the flag's spelling is different to the filter `reverse`)
Available variables:
- forloop.name
-
‘item-collection’
- forloop.length
-
Length of the loop
- forloop.index
-
The current item’s position in the collection; forloop.index starts at 1. This is helpful for non-programmers who start believe the first item in an array is 1, not 0.
- forloop.index0
-
The current item’s position in the collection where the first item is 0
- forloop.rindex
-
Number of items remaining in the loop (length - index) where 1 is the last item.
- forloop.rindex0
-
Number of items remaining in the loop where 0 is the last item.
- forloop.first
-
Returns true if the item is the first item.
- forloop.last
-
Returns true if the item is the last item.
- forloop.parentloop
-
Provides access to the parent loop, if present.
Defined Under Namespace
Classes: ParseTreeVisitor
Constant Summary collapse
- Syntax =
/\A(#{VariableSegment}+)\s+in\s+(#{QuotedFragment}+)\s*(reversed)?/o
Constants inherited from Block
Instance Attribute Summary collapse
-
#collection_name ⇒ Object
readonly
Returns the value of attribute collection_name.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#variable_name ⇒ Object
readonly
Returns the value of attribute variable_name.
Attributes inherited from Tag
#line_number, #parse_context, #tag_name
Instance Method Summary collapse
-
#initialize(tag_name, markup, options) ⇒ For
constructor
A new instance of For.
- #nodelist ⇒ Object
- #parse(tokens) ⇒ Object
- #render_to_output_buffer(context, output) ⇒ Object
- #unknown_tag(tag, markup, tokens) ⇒ Object
Methods inherited from Block
#blank?, #block_delimiter, #block_name, #raise_tag_never_closed, raise_unknown_tag, #render
Methods inherited from Tag
#blank?, disable_tags, #name, parse, #raw, #render
Methods included from ParserSwitching
#parse_with_selected_parser, #strict_parse_with_error_mode_fallback
Constructor Details
#initialize(tag_name, markup, options) ⇒ For
Returns a new instance of For.
53 54 55 56 57 58 59 |
# File 'lib/liquid/tags/for.rb', line 53 def initialize(tag_name, markup, ) super @from = @limit = nil parse_with_selected_parser(markup) @for_block = new_body @else_block = nil end |
Instance Attribute Details
#collection_name ⇒ Object (readonly)
Returns the value of attribute collection_name.
51 52 53 |
# File 'lib/liquid/tags/for.rb', line 51 def collection_name @collection_name end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
51 52 53 |
# File 'lib/liquid/tags/for.rb', line 51 def from @from end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
51 52 53 |
# File 'lib/liquid/tags/for.rb', line 51 def limit @limit end |
#variable_name ⇒ Object (readonly)
Returns the value of attribute variable_name.
51 52 53 |
# File 'lib/liquid/tags/for.rb', line 51 def variable_name @variable_name end |
Instance Method Details
#nodelist ⇒ Object
73 74 75 |
# File 'lib/liquid/tags/for.rb', line 73 def nodelist @else_block ? [@for_block, @else_block] : [@for_block] end |
#parse(tokens) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/liquid/tags/for.rb', line 61 def parse(tokens) if parse_body(@for_block, tokens) parse_body(@else_block, tokens) end if blank? @else_block&.remove_blank_strings @for_block.remove_blank_strings end @else_block&.freeze @for_block.freeze end |
#render_to_output_buffer(context, output) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/liquid/tags/for.rb', line 82 def render_to_output_buffer(context, output) segment = collection_segment(context) if segment.empty? render_else(context, output) else render_segment(context, output, segment) end output end |
#unknown_tag(tag, markup, tokens) ⇒ Object
77 78 79 80 |
# File 'lib/liquid/tags/for.rb', line 77 def unknown_tag(tag, markup, tokens) return super unless tag == 'else' @else_block = new_body end |