Class: Archangel::Liquid::Tags::CollectionTag

Inherits:
ApplicationTag
  • Object
show all
Defined in:
lib/archangel/liquid/tags/collection_tag.rb

Overview

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 %}

Constant Summary collapse

SYNTAX =

Regex for tag syntax

/
  (?<key>#{::Liquid::VariableSignature}+)
  \s*
  =
  \s*
  (?<value>#{::Liquid::QuotedFragment}+)
  \s*
  (?<attributes>.*)
  \s*
/omx.freeze

Constants inherited from ApplicationTag

ApplicationTag::ASSET_ATTRIBUTES_SYNTAX, ApplicationTag::ASSET_SYNTAX, ApplicationTag::KEY_VALUE_ATTRIBUTES_SYNTAX, ApplicationTag::SLUG_ATTRIBUTES_SYNTAX, ApplicationTag::SLUG_SYNTAX, ApplicationTag::URL_ATTRIBUTES_SYNTAX

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ CollectionTag

Collection for Liquid

Parameters:

  • tag_name (String)

    the Liquid tag name

  • markup (String)

    the passed options

  • options (Object)

    options


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)

78
79
80
# File 'lib/archangel/liquid/tags/collection_tag.rb', line 78

def blank?
  true
end

#render(context) ⇒ Hash

Render the collection object

Parameters:

  • context (Object)

    the Liquid context

Returns:

  • (Hash)

    the object


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/archangel/liquid/tags/collection_tag.rb', line 66

def render(context)
  environments = context.environments.first
  site = environments["site"]

  val = load_collection(site)

  context.scopes.last[key] = val
  context.resource_limits.assign_score += assign_score_of(val)

  ""
end