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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ Snippet

Returns a new instance of Snippet.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/locomotive/liquid/tags/snippet.rb', line 10

def initialize(tag_name, markup, tokens, context)
  super

  @slug = @template_name.gsub(/['"]/o, '')

  if @context[:snippets].present?
    (@context[:snippets] << @slug).uniq!
  else
    @context[:snippets] = [@slug]
  end

  if @context[:site].present?
    snippet = @context[:site].snippets.where(:slug => @slug).first
    self.refresh(snippet) if snippet
  end
end

Instance Attribute Details

#partialObject

Returns the value of attribute partial.



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

def partial
  @partial
end

#slugObject

Returns the value of attribute slug.



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

def slug
  @slug
end

Instance Method Details

#nodelistObject



62
63
64
65
66
67
68
# File 'lib/locomotive/liquid/tags/snippet.rb', line 62

def nodelist
  if @partial
    @partial.root.nodelist
  else
    []
  end
end

#refresh(snippet, context = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/locomotive/liquid/tags/snippet.rb', line 51

def refresh(snippet, context = {})
  if snippet.destroyed?
    @snippet_id = nil
    @partial = nil
  else
    @snippet_id = snippet.id
    @partial = ::Liquid::Template.parse(snippet.template, @context.clone)
    @partial.root.context.clear
  end
end

#render(context) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/locomotive/liquid/tags/snippet.rb', line 27

def render(context)
  return '' if @partial.nil?

  variable = context[@variable_name || @template_name[1..-2]]

  context.stack do
    @attributes.each do |key, value|
      context[key] = context[value]
    end

    output = (if variable.is_a?(Array)
      variable.collect do |variable|
        context[@template_name[1..-2]] = variable
        @partial.render(context)
      end
    else
      context[@template_name[1..-2]] = variable
      @partial.render(context)
    end)

    output
  end
end