Class: Liquid::Variable

Inherits:
Object
  • Object
show all
Includes:
ParserSwitching
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 =
/(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ParserSwitching

#parse_with_selected_parser

Constructor Details

#initialize(markup, options = {}) ⇒ Variable

Returns a new instance of Variable.



19
20
21
22
23
24
25
# File 'lib/liquid/variable.rb', line 19

def initialize(markup, options = {})
  @markup  = markup
  @name    = nil
  @options = options || {}

  parse_with_selected_parser(markup)
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



15
16
17
# File 'lib/liquid/variable.rb', line 15

def filters
  @filters
end

#line_numberObject

Returns the value of attribute line_number.



16
17
18
# File 'lib/liquid/variable.rb', line 16

def line_number
  @line_number
end

#nameObject

Returns the value of attribute name.



15
16
17
# File 'lib/liquid/variable.rb', line 15

def name
  @name
end

#warningsObject

Returns the value of attribute warnings.



15
16
17
# File 'lib/liquid/variable.rb', line 15

def warnings
  @warnings
end

Instance Method Details

#lax_parse(markup) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/liquid/variable.rb', line 35

def lax_parse(markup)
  @filters = []
  if markup =~ /(#{QuotedFragment})(.*)/om
    name_markup = $1
    filter_markup = $2
    @name = Expression.parse(name_markup)
    if filter_markup =~ /#{FilterSeparator}\s*(.*)/om
      filters = $1.scan(FilterParser)
      filters.each do |f|
        if f =~ /\w+/
          filtername = Regexp.last_match(0)
          filterargs = f.scan(/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o).flatten
          @filters << parse_filter_expressions(filtername, filterargs)
        end
      end
    end
  end
end

#markup_context(markup) ⇒ Object



31
32
33
# File 'lib/liquid/variable.rb', line 31

def markup_context(markup)
  "in \"{{#{markup}}}\""
end

#parse_filterargs(p) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/liquid/variable.rb', line 67

def parse_filterargs(p)
  # first argument
  filterargs = [p.argument]
  # followed by comma separated others
  while p.consume?(:comma)
    filterargs << p.argument
  end
  filterargs
end

#rawObject



27
28
29
# File 'lib/liquid/variable.rb', line 27

def raw
  @markup
end

#render(context) ⇒ Object



77
78
79
80
81
82
# File 'lib/liquid/variable.rb', line 77

def render(context)
  @filters.inject(context.evaluate(@name)) do |output, (filter_name, filter_args, filter_kwargs)|
    filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
    output = context.invoke(filter_name, output, *filter_args)
  end.tap{ |obj| taint_check(obj) }
end

#strict_parse(markup) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/liquid/variable.rb', line 54

def strict_parse(markup)
  @filters = []
  p = Parser.new(markup)

  @name = Expression.parse(p.expression)
  while p.consume?(:pipe)
    filtername = p.consume(:id)
    filterargs = p.consume?(:colon) ? parse_filterargs(p) : []
    @filters << parse_filter_expressions(filtername, filterargs)
  end
  p.consume(:end_of_string)
end