Class: Liquid::Variable
- Inherits:
-
Object
- Object
- Liquid::Variable
- 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
-
#filters ⇒ Object
Returns the value of attribute filters.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#initialize(markup) ⇒ Variable
constructor
A new instance of Variable.
- #render(context) ⇒ Object
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
#filters ⇒ Object
Returns the value of attribute filters.
6 7 8 |
# File 'lib/liquid/variable.rb', line 6 def filters @filters end |
#name ⇒ Object
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 |