Class: Solid::LiquidExtensions::ForTag
- Extended by:
- TagHighjacker
- Defined in:
- lib/solid/liquid_extensions/for_tag.rb
Overview
This for block reimplementation is deliberately backward incompatible since all strange features supported by the original for loop like “reversed” or “limit: 20 offset: 40” are favourably replaced by pure ruby methods like ‘Array#reversed` or `Array#slice`
Instance Method Summary collapse
- #display(collection) ⇒ Object
-
#initialize(tag_name, expression, tokens, context = {}) ⇒ ForTag
constructor
A new instance of ForTag.
Methods included from TagHighjacker
Methods inherited from Block
Methods included from Element
#arguments, #current_context, included, #with_context
Constructor Details
#initialize(tag_name, expression, tokens, context = {}) ⇒ ForTag
Returns a new instance of ForTag.
13 14 15 16 |
# File 'lib/solid/liquid_extensions/for_tag.rb', line 13 def initialize(tag_name, expression, tokens, context = {}) @variable_name, iterable_expression = expression.split(/\s+in\s+/, 2).map(&:strip) super(tag_name, iterable_expression, tokens, context) end |
Instance Method Details
#display(collection) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/solid/liquid_extensions/for_tag.rb', line 18 def display(collection) forloop = loop_for(collection) output = [] collection = [] unless collection.respond_to?(:each_with_index) collection.each_with_index do |element, index| current_context.stack do current_context[@variable_name] = element.to_liquid current_context['forloop'] = forloop output << yield forloop.inc! end end output.join end |