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, parse_context) ⇒ Variable

Returns a new instance of Variable.



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

def initialize(markup, parse_context)
  @markup  = markup
  @name    = nil
  @parse_context = parse_context
  @line_number = parse_context.line_number

  parse_with_selected_parser(markup)
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



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

def filters
  @filters
end

#line_numberObject

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parse_contextObject (readonly) Also known as: options

Returns the value of attribute parse_context.



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

def parse_context
  @parse_context
end

Instance Method Details

#lax_parse(markup) ⇒ Object



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

def lax_parse(markup)
  @filters = []
  return unless 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|
      next unless 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

#markup_context(markup) ⇒ Object



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

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

#parse_filterargs(p) ⇒ Object



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

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

#rawObject



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

def raw
  @markup
end

#render(context) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/liquid/variable.rb', line 75

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

  obj = context.apply_global_filter(obj)

  taint_check(context, obj)

  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