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 }}

Defined Under Namespace

Classes: ParseTreeVisitor

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, #strict_parse_with_error_mode_fallback

Constructor Details

#initialize(markup, parse_context) ⇒ Variable

Returns a new instance of Variable.



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

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

  strict_parse_with_error_mode_fallback(markup)
end

Instance Attribute Details

#filtersObject

Returns the value of attribute filters.



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

def filters
  @filters
end

#line_numberObject

Returns the value of attribute line_number.



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

def line_number
  @line_number
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#parse_contextObject (readonly) Also known as: options

Returns the value of attribute parse_context.



22
23
24
# File 'lib/liquid/variable.rb', line 22

def parse_context
  @parse_context
end

Instance Method Details

#disabled?(_context) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/liquid/variable.rb', line 115

def disabled?(_context)
  false
end

#disabled_tagsObject



119
120
121
# File 'lib/liquid/variable.rb', line 119

def disabled_tags
  []
end

#lax_parse(markup) ⇒ Object



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

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

  name_markup   = Regexp.last_match(1)
  filter_markup = Regexp.last_match(2)
  @name         = parse_context.parse_expression(name_markup)
  if filter_markup =~ FilterMarkupRegex
    filters = Regexp.last_match(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



40
41
42
# File 'lib/liquid/variable.rb', line 40

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

#parse_filterargs(p) ⇒ Object



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

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

#rawObject



36
37
38
# File 'lib/liquid/variable.rb', line 36

def raw
  @markup
end

#render(context) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/liquid/variable.rb', line 85

def render(context)
  obj = context.evaluate(@name)

  @filters.each do |filter_name, filter_args, filter_kwargs|
    filter_args = evaluate_filter_expressions(context, filter_args, filter_kwargs)
    obj = context.invoke(filter_name, obj, *filter_args)
  end

  context.apply_global_filter(obj)
end

#render_obj_to_output(obj, output) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/liquid/variable.rb', line 102

def render_obj_to_output(obj, output)
  case obj
  when NilClass
    # Do nothing
  when Array
    obj.each do |o|
      render_obj_to_output(o, output)
    end
  when
    output << obj.to_s
  end
end

#render_to_output_buffer(context, output) ⇒ Object



96
97
98
99
100
# File 'lib/liquid/variable.rb', line 96

def render_to_output_buffer(context, output)
  obj = render(context)
  render_obj_to_output(obj, output)
  output
end

#strict_parse(markup) ⇒ Object



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

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

  return if p.look(:end_of_string)

  @name = parse_context.parse_expression(p.expression)
  while p.consume?(:pipe)
    filtername = p.consume(:id)
    filterargs = p.consume?(:colon) ? parse_filterargs(p) : Const::EMPTY_ARRAY
    @filters << parse_filter_expressions(filtername, filterargs)
  end
  p.consume(:end_of_string)
end