Class: Archangel::Liquid::Tags::WidgetTag

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

Overview

Widget custom tag for Liquid

Example

{% widget "widget-name" %}
{% widget 'widget-name' %}
{% widget widget-name %}

Constant Summary

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) ⇒ WidgetTag

Widget for Liquid


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/archangel/liquid/tags/widget_tag.rb', line 22

def initialize(tag_name, markup, options)
  super

  match = SLUG_SYNTAX.match(markup)

  if match.blank?
    raise ::Liquid::SyntaxError, Archangel.t("errors.syntax.widget")
  end

  @slug = ::Liquid::Variable.new(match[:slug], options).name
end

Instance Method Details

#render(context) ⇒ String

Render the Widget


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/archangel/liquid/tags/widget_tag.rb', line 40

def render(context)
  return if slug.blank?

  environments = context.environments.first
  site = environments["site"]

  widget = load_widget_for(site)

  return if widget.blank?

  rendered_widget = render_widget(widget.content, environments)

  if widget.design.present?
    rendered_widget = render_designed_widget(widget.design,
                                             rendered_widget)
  end

  rendered_widget
end