Class: Locomotive::Steam::Liquid::Tags::Snippet

Inherits:
Liquid::Include
  • Object
show all
Defined in:
lib/locomotive/steam/liquid/tags/snippet.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ Snippet

Returns a new instance of Snippet.



10
11
12
13
14
15
16
# File 'lib/locomotive/steam/liquid/tags/snippet.rb', line 10

def initialize(tag_name, markup, options)
  super

  if markup =~ Syntax
    @template_name = $1
  end
end

Instance Attribute Details

#template_nameObject (readonly)

Returns the value of attribute template_name.



8
9
10
# File 'lib/locomotive/steam/liquid/tags/snippet.rb', line 8

def template_name
  @template_name
end

Instance Method Details

#parse(tokens) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/locomotive/steam/liquid/tags/snippet.rb', line 18

def parse(tokens)
  ActiveSupport::Notifications.instrument('steam.parse.include', page: parse_context[:page], name: template_name)

  # look for editable elements (only used by the Engine)
  # In the next version of Locomotive (v5), we won't support the editable elements
  #
  # NOTE: it doesn't support dynamically choosen template
  #
  if parse_context[:snippet_finder] && snippet = parse_context[:snippet_finder].find(template_name)
    parse_context[:parser]._parse(snippet, parse_context.merge(snippet: template_name))
  end
end

#render(context) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/locomotive/steam/liquid/tags/snippet.rb', line 31

def render(context)
  # parse_context (previously @options) doesn't include the page key if cache is on
  parse_context[:page] = context.registers[:page]

  begin
    super
  rescue ::Liquid::ArgumentError
    # NOTE: Locomotive site developers should always use quotes (or doubles quotes) for the name of a snippet.
    # Unfortunately, a lot of sites don't use them. So here is a little patch to not break those sites.
    Locomotive::Common::Logger.warn("Use quotes if the name of your snippet (#{template_name}) is not dynamic.")

    @template_name_expr = template_name

    super
  end
end