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

FilterMarkupRegex =
/#{FilterSeparator}\s*(.*)/om
FilterParser =
/(?:\s+|#{QuotedFragment}|#{ArgumentSeparator})+/o
FilterArgsRegex =
/(?:#{FilterArgumentSeparator}|#{ArgumentSeparator})\s*((?:\w+\s*\:\s*)?#{QuotedFragment})/o
JustTagAttributes =
/\A#{TagAttributes}\z/o
MarkupWithQuotedFragment =
/(#{QuotedFragment})(.*)/om

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.



25
26
27
28
29
30
31
32
# File 'lib/liquid/variable.rb', line 25

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.



19
20
21
# File 'lib/liquid/variable.rb', line 19

def filters
  @filters
end

#line_numberObject

Returns the value of attribute line_number.



19
20
21
# File 'lib/liquid/variable.rb', line 19

def line_number
  @line_number
end

#nameObject

Returns the value of attribute name.



19
20
21
# File 'lib/liquid/variable.rb', line 19

def name
  @name
end

#parse_contextObject (readonly) Also known as: options

Returns the value of attribute parse_context.



20
21
22
# File 'lib/liquid/variable.rb', line 20

def parse_context
  @parse_context
end

Instance Method Details

#lax_parse(markup) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/liquid/variable.rb', line 42

def lax_parse(markup)
  @filters = []
  return unless markup =~ MarkupWithQuotedFragment

  name_markup = $1
  filter_markup = $2
  @name = Expression.parse(name_markup)
  if filter_markup =~ FilterMarkupRegex
    filters = $1.scan(FilterParser)
    filters.each do |f|
      next unless f =~ /\w+/
      filtername = Regexp.last_match(0)
      filterargs = f.scan(FilterArgsRegex).flatten
      @filters << parse_filter_expressions(filtername, filterargs)
    end
  end
end

#markup_context(markup) ⇒ Object



38
39
40
# File 'lib/liquid/variable.rb', line 38

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

#parse_filterargs(p) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/liquid/variable.rb', line 73

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

#rawObject



34
35
36
# File 'lib/liquid/variable.rb', line 34

def raw
  @markup
end

#render(context) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/liquid/variable.rb', line 81

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



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/liquid/variable.rb', line 60

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