Class: Liquid::Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/liquid/variable.rb

Overview

Hols variables. Variables are only loaded “just in time” they are not evaluated as part of the render stage

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(markup) ⇒ Variable

Returns a new instance of Variable.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/liquid/variable.rb', line 8

def initialize(markup)
  @markup = markup                            
  @name = markup.match(/\s*(#{QuotedFragment})/)[1]
  if markup.match(/#{FilterSperator}\s*(.*)/)
    filters = Regexp.last_match(1).split(/#{FilterSperator}/)
    
    @filters = filters.collect do |f|          
      filtername = f.match(/\s*(\w+)/)[1]
      filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/).flatten            
      [filtername.to_sym, filterargs]
    end
  else
    @filters = []
  end
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



6
7
8
# File 'lib/liquid/variable.rb', line 6

def filters
  @filters
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/liquid/variable.rb', line 6

def name
  @name
end

Instance Method Details

#render(context) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/liquid/variable.rb', line 24

def render(context)      
  output = context[@name]
  @filters.inject(output) do |output, filter|
    filterargs = filter[1].to_a.collect do |a|
     context[a]
    end
    begin
      output = context.invoke(filter[0], output, *filterargs)
    rescue FilterNotFound
      raise FilterNotFound, "Error - filter '#{filter[0]}' in '#{@markup.strip}' could not be found."
    end
  end  
  output
end