Class: Archangel::Liquid::Tags::AssetTag

Inherits:
ApplicationTag show all
Includes:
ActionView::Helpers::UrlHelper
Defined in:
lib/archangel/liquid/tags/asset_tag.rb

Overview

Asset custom tag for Liquid

Example

{% asset 'my-asset.png' %} #=>
  <img src="path/to/my-asset.png" alt:"my-asset.png">
{% asset 'my-asset.png' size:'medium' %} #=>
  <img src="path/to/medium_my-asset.png" alt:"my-asset.png">
{% asset 'my-asset.png' alt:'My image' class:'center' %} #=>
  <img src="path/to/my-asset.png" alt="My image" class="center">

Constant Summary

Constants inherited from ApplicationTag

Archangel::Liquid::Tags::ApplicationTag::ASSET_ATTRIBUTES_SYNTAX, Archangel::Liquid::Tags::ApplicationTag::ASSET_SYNTAX, Archangel::Liquid::Tags::ApplicationTag::KEY_VALUE_ATTRIBUTES_SYNTAX, Archangel::Liquid::Tags::ApplicationTag::SLUG_ATTRIBUTES_SYNTAX, Archangel::Liquid::Tags::ApplicationTag::SLUG_SYNTAX, Archangel::Liquid::Tags::ApplicationTag::URL_ATTRIBUTES_SYNTAX

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ AssetTag

Asset for Liquid


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/archangel/liquid/tags/asset_tag.rb', line 27

def initialize(tag_name, markup, options)
  super

  match = ASSET_ATTRIBUTES_SYNTAX.match(markup)

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

  @key = ::Liquid::Variable.new(match[:asset], options).name
  @attributes = {}

  build_attributes(match[:attributes])
end

Instance Method Details

#render(context) ⇒ String

Render the Asset


48
49
50
51
52
53
54
55
56
57
# File 'lib/archangel/liquid/tags/asset_tag.rb', line 48

def render(context)
  return if key.blank?

  environments = context.environments[0]
  asset = load_asset_for(environments["site"])

  return if asset.blank?

  asset_decider(asset)
end