class Liquid::Variable

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

Constants

FilterArgsRegex
FilterMarkupRegex
FilterParser
JustTagAttributes
MarkupWithQuotedFragment

Attributes

filters[RW]
line_number[RW]
name[RW]
options[R]
parse_context[R]

Public Class Methods

new(markup, parse_context) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 27
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

Public Instance Methods

disabled?(_context) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 107
def disabled?(_context)
  false
end
disabled_tags() click to toggle source
# File lib/liquid-render-tag/variable.rb, line 111
def disabled_tags
  []
end
lax_parse(markup) click to toggle source
# File lib/liquid-render-tag/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         = Expression.parse(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) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 40
def markup_context(markup)
  "in \"{{#{markup}}}\""
end
parse_filterargs(p) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 75
def parse_filterargs(p)
  # first argument
  filterargs = [p.argument]
  # followed by comma separated others
  filterargs << p.argument while p.consume?(:comma)
  filterargs
end
raw() click to toggle source
# File lib/liquid-render-tag/variable.rb, line 36
def raw
  @markup
end
render(context) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 83
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
render_to_output_buffer(context, output) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 94
def render_to_output_buffer(context, output)
  obj = render(context)

  if obj.is_a?(Array)
    output << obj.join
  elsif obj.nil?
  else
    output << obj.to_s
  end

  output
end
strict_parse(markup) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 62
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

Private Instance Methods

evaluate_filter_expressions(context, filter_args, filter_kwargs) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 133
def evaluate_filter_expressions(context, filter_args, filter_kwargs)
  parsed_args = filter_args.map { |expr| context.evaluate(expr) }
  if filter_kwargs
    parsed_kwargs = {}
    filter_kwargs.each do |key, expr|
      parsed_kwargs[key] = context.evaluate(expr)
    end
    parsed_args << parsed_kwargs
  end
  parsed_args
end
parse_filter_expressions(filter_name, unparsed_args) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 117
def parse_filter_expressions(filter_name, unparsed_args)
  filter_args  = []
  keyword_args = nil
  unparsed_args.each do |a|
    if (matches = a.match(JustTagAttributes))
      keyword_args           ||= {}
      keyword_args[matches[1]] = Expression.parse(matches[2])
    else
      filter_args << Expression.parse(a)
    end
  end
  result = [filter_name, filter_args]
  result << keyword_args if keyword_args
  result
end
taint_check(context, obj) click to toggle source
# File lib/liquid-render-tag/variable.rb, line 145
def taint_check(context, obj)
  return if Template.taint_mode == :lax
  return unless obj.tainted?

  @markup =~ QuotedFragment
  name = Regexp.last_match(0)

  error               = TaintedError.new("variable '#{name}' is tainted and was not escaped")
  error.line_number   = line_number
  error.template_name = context.template_name

  case Template.taint_mode
  when :warn
    context.warnings << error
  when :error
    raise error
  end
end