class Liquid::Context

Context keeps the variable stack and resolves variables, as well as keywords

context['variable'] = 'testing'
context['variable'] #=> 'testing'
context['true']     #=> true
context['10.2232']  #=> 10.2232

context.stack do
   context['bob'] = 'bobsen'
end

context['bob']  #=> nil  class Context

Constants

LITERALS

Attributes

environments[R]
errors[R]
registers[R]
scopes[R]

Public Class Methods

new(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false) click to toggle source
# File lib/liquid/context.rb, line 18
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false)
  @environments   = [environments].flatten
  @scopes         = [(outer_scope || {})]
  @registers      = registers
  @errors         = []
  @rethrow_errors = rethrow_errors
  squash_instance_assigns_with_environments
end

Public Instance Methods

[](key) click to toggle source
# File lib/liquid/context.rb, line 105
def [](key)
  resolve(key)
end
[]=(key, value) click to toggle source

Only allow String, Numeric, Hash, Array, Proc, Boolean or Liquid::Drop

# File lib/liquid/context.rb, line 101
def []=(key, value)
  @scopes[0][key] = value
end
add_filters(filters) click to toggle source

Adds filters to this context.

Note that this does not register the filters with the main Template object. see Template.register_filter for that

# File lib/liquid/context.rb, line 35
def add_filters(filters)
  filters = [filters].flatten.compact

  filters.each do |f|
    raise ArgumentError, "Expected module but got: #{f.class}" unless f.is_a?(Module)
    strainer.extend(f)
  end
end
clear_instance_assigns() click to toggle source
# File lib/liquid/context.rb, line 96
def clear_instance_assigns
  @scopes[0] = {}
end
handle_error(e) click to toggle source
# File lib/liquid/context.rb, line 44
def handle_error(e)
  errors.push(e)
  raise if @rethrow_errors

  case e
  when SyntaxError
    "Liquid syntax error: #{e.message}"
  else
    "Liquid error: #{e.message}"
  end
end
has_key?(key) click to toggle source
# File lib/liquid/context.rb, line 109
def has_key?(key)
  resolve(key) != nil
end
invoke(method, *args) click to toggle source
# File lib/liquid/context.rb, line 56
def invoke(method, *args)
  if strainer.respond_to?(method)
    strainer.__send__(method, *args)
  else
    args.first
  end
end
merge(new_scopes) click to toggle source

Merge a hash of variables in the current local scope

# File lib/liquid/context.rb, line 71
def merge(new_scopes)
  @scopes[0].merge!(new_scopes)
end
pop() click to toggle source

Pop from the stack. use Context#stack instead

# File lib/liquid/context.rb, line 76
def pop
  raise ContextError if @scopes.size == 1
  @scopes.shift
end
push(new_scope={}) click to toggle source

Push new local scope on the stack. use Context#stack instead

# File lib/liquid/context.rb, line 65
def push(new_scope={})
  @scopes.unshift(new_scope)
  raise StackLevelError, "Nesting too deep" if @scopes.length > 100
end
stack(new_scope={}) { || ... } click to toggle source

Pushes a new local scope on the stack, pops it at the end of the block

Example:

context.stack do
   context['var'] = 'hi'
end

context['var]  #=> nil
# File lib/liquid/context.rb, line 89
def stack(new_scope={})
  push(new_scope)
  yield
ensure
  pop
end
strainer() click to toggle source
# File lib/liquid/context.rb, line 27
def strainer
  @strainer ||= Strainer.create(self)
end