class Archangel::Liquid::Tags::CollectionforTag

Collection custom tag for Liquid to set a variable for Collections

Example

{% collectionfor item in 'my-collection' %}
  {{ forloop.index }}: {{ item.name }}
{% endcollectionfor %}

{% collectionfor item in 'my-collection' limit:5 offset:25 %}
  {{ forloop.index }}: {{ item.name }}
{% endcollectionfor %}

{% collectionfor item in 'my-collection' reversed %}
  {{ forloop.index }}: {{ item.name }}
{% endcollectionfor %}

{% collectionfor item in 'my-collection' %}
  {{ forloop.index }}: {{ item.name }}
{% else %}
   There is nothing in the collection.
{% endcollectionfor %}

{% collectionfor item in 'my-collection' %}
  {{ forloop.name }}
  {{ forloop.length }}
  {{ forloop.index }}
  {{ forloop.index0 }}
  {{ forloop.rindex }}
  {{ forloop.rindex0 }}
  {{ forloop.first }}
  {{ forloop.last }}
  {{ forloop.parentloop }}
{% endcollectionfor %}

Protected Instance Methods

collection_segment(context) click to toggle source
# File lib/archangel/liquid/tags/collectionfor_tag.rb, line 43
def collection_segment(context)
  offsets = context.registers[:for] ||= {}
  offset = context.evaluate(@from).to_i
  limit = context.evaluate(@limit)

  segment = load_collection(context["site"].object, offset, limit)

  segment.reverse! if @reversed

  offsets[@name] = offset + segment.length

  segment
end
default_values_for(entry) click to toggle source
# File lib/archangel/liquid/tags/collectionfor_tag.rb, line 81
def default_values_for(entry)
  { "id" => entry["id"] }
end
load_collection(site, offset, limit) click to toggle source
# File lib/archangel/liquid/tags/collectionfor_tag.rb, line 57
def load_collection(site, offset, limit)
  entries = load_collection_entries_for(site, offset, limit)

  entries.each_with_object([]) do |entry, collection|
    collection <<
      default_values_for(entry).reverse_merge(entry["value"])
  end
end
load_collection_entries_for(site, offset, limit) click to toggle source
# File lib/archangel/liquid/tags/collectionfor_tag.rb, line 66
def load_collection_entries_for(site, offset, limit)
  collection = site.collections.find_by!(slug: @collection_name)

  offset = 1 if offset.zero?
  limit = 12 if limit.blank?

  site.entries
      .available
      .where(collection: collection)
      .page(offset).per(limit)
      .map(&:attributes)
rescue StandardError
  []
end