class Archangel::Liquid::Tags::CollectionTag
Collection custom tag for Liquid
to set a variable for Collections
Example
{% collection things = 'my-collection' %} {% for item in things %} {{ forloop.index }}: {{ item.name }} {% endfor %} {% collection things = 'my-collection' limit:5 offset:25 %} {% for item in things %} {{ forloop.index }}: {{ item.name }} {% endfor %}
Constants
- SYNTAX
Regex for tag syntax
Attributes
attributes[R]
key[R]
value[R]
Public Class Methods
new(tag_name, markup, options)
click to toggle source
Collection for Liquid
@param tag_name [String] the Liquid
tag name @param markup [String] the passed options @param options [Object] options
Calls superclass method
# File lib/archangel/liquid/tags/collection_tag.rb, line 42 def initialize(tag_name, markup, options) super match = SYNTAX.match(markup) if match.blank? raise ::Liquid::SyntaxError, Archangel.t("errors.syntax.collection") end @key = match[:key] @value = ::Liquid::Variable.new(match[:value], options).name @attributes = {} match[:attributes].scan(KEY_VALUE_ATTRIBUTES_SYNTAX) do |key, value| @attributes[key.to_sym] = ::Liquid::Expression.parse(value) end end
Public Instance Methods
blank?()
click to toggle source
# File lib/archangel/liquid/tags/collection_tag.rb, line 75 def blank? true end
render(context)
click to toggle source
Render the collection object
@param context [Object] the Liquid
context @return [Hash] the object
# File lib/archangel/liquid/tags/collection_tag.rb, line 66 def render(context) val = load_collection(context["site"].object) context.scopes.last[key] = val context.resource_limits.assign_score << assign_score_of(val) "" end
Protected Instance Methods
assign_score_of(val)
click to toggle source
# File lib/archangel/liquid/tags/collection_tag.rb, line 105 def assign_score_of(val) return val.length if val.instance_of?(String) return 1 unless val.instance_of?(Array) || val.instance_of?(Hash) val.inject(1) { |sum, item| sum + assign_score_of(item) } end
default_values(entry)
click to toggle source
# File lib/archangel/liquid/tags/collection_tag.rb, line 113 def default_values(entry) { "id" => entry.fetch("id", 0) } end
load_collection(site)
click to toggle source
# File lib/archangel/liquid/tags/collection_tag.rb, line 83 def load_collection(site) items = load_collection_for(site, value) items.each_with_object([]) do |item, collection| collection << default_values(item).reverse_merge(item.fetch("value")) end end
load_collection_for(site, slug)
click to toggle source
# File lib/archangel/liquid/tags/collection_tag.rb, line 92 def load_collection_for(site, slug) collection = site.collections.find_by!(slug: slug) site.entries .available .where(collection: collection) .page(attributes.fetch(:offset, 1)) .per(attributes.fetch(:limit, 12)) .map(&:attributes) rescue StandardError [] end