Class: Liquid::Variable
- Inherits:
-
Object
- Object
- Liquid::Variable
- Defined in:
- lib/liquid/variable.rb
Overview
Holds variables. Variables are only loaded “just in time” and are not evaluated as part of the render stage
{{ monkey }}
{{ user.name }}
Variables can be combined with filters:
{{ user | link }}
Constant Summary collapse
- FilterParser =
/(?:#{FilterSeparator}|(?:\s*(?!(?:#{FilterSeparator}))(?:#{QuotedFragment}|\S+)\s*)+)/o
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.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/liquid/variable.rb', line 17 def initialize(markup) @markup = markup @name = nil @filters = [] if match = markup.match(/\s*(#{QuotedFragment})(.*)/o) @name = match[1] if match[2].match(/#{FilterSeparator}\s*(.*)/o) filters = Regexp.last_match(1).scan(FilterParser) filters.each do |f| if matches = f.match(/\s*(\w+)/) filtername = matches[1] filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*(#{QuotedFragment})/o).flatten @filters << [filtername.to_sym, filterargs] end end end end end |
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
15 16 17 |
# File 'lib/liquid/variable.rb', line 15 def filters @filters end |
#name ⇒ Object
Returns the value of attribute name.
15 16 17 |
# File 'lib/liquid/variable.rb', line 15 def name @name end |
Instance Method Details
#render(context) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/liquid/variable.rb', line 36 def render(context) return '' if @name.nil? @filters.inject(context[@name]) 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 end |