class Mustache::Context


executed within. All Mustache tags reference keys in the Context.
A Context represents the context which a Mustache template is

def [](name)

Alias for `fetch`.
def [](name)
  fetch(name, nil)
end

def []=(name, value)

context[:name] = "Chris"

Can be used to add a value to the context in a hash-like way.
def []=(name, value)
  push(name => value)
end

def current

def current
  @stack.first
end

def escape(value)

Returns:
  • (String) - Escaped string.

Parameters:
  • value (Object) -- Value to escape.
def escape(value)
  mustache_in_stack.escape(value)
end

def fetch(name, default = :__raise)

set to true), will raise a ContextMiss exception on miss.
If no second parameter is passed (or raise_on_context_miss is

second parameter.
stack. You may specify the default return value by passing a
Similar to Hash#fetch, finds a value by `name` in the context's
def fetch(name, default = :__raise)
  @stack.each do |frame|
    # Prevent infinite recursion.
    next if frame == self
    value = find(frame, name, :__missing)
    return value if :__missing != value
  end
  if default == :__raise || mustache_in_stack.raise_on_context_miss?
    raise ContextMiss.new("Can't find #{name} in #{@stack.inspect}")
  else
    default
  end
end

def find(obj, key, default = nil)

Returns:
  • (Object) - The value of key in object if it is found, and default otherwise.

Parameters:
  • default (Object) -- An optional default value, to return if the key is not found.
  • key (String, Symbol) -- The key whose value you want
  • obj (Object) -- The object to perform the lookup on.
def find(obj, key, default = nil)
  return find_in_hash(obj.to_hash, key, default) if obj.respond_to?(:to_hash)
  unless obj.respond_to?(key)
    # no match for the key, but it may include a hyphen, so try again replacing hyphens with underscores.
    key = key.to_s.tr('-', '_')
    return default unless obj.respond_to?(key)
  end
  meth = obj.method(key) rescue proc { obj.send(key) }
  meth.arity == 1 ? meth.to_proc : meth.call
end

def find_in_hash(obj, key, default)

Fetches a hash key if it exists, or returns the given default.
def find_in_hash(obj, key, default)
  return obj[key]      if obj.has_key?(key)
  return obj[key.to_s] if obj.has_key?(key.to_s)
  return obj[key]      if obj.respond_to?(:default_proc) && obj.default_proc && obj[key]
  # If default is :__missing then we are from #fetch which is hunting through the stack
  # If default is nil then we are reducing dot notation
  if :__missing != default && mustache_in_stack.raise_on_context_miss?
    raise ContextMiss.new("Can't find #{key} in #{obj}")
  else
    obj.fetch(key, default)
  end
end

def has_key?(key)

`context[key]` give us a result that was set. Basically.
Do we know about a particular key? In other words, will calling
def has_key?(key)
  fetch(key, false)
rescue ContextMiss
  false
end

def initialize(mustache)

Parameters:
  • mustache (Mustache) -- A Mustache instance.
def initialize(mustache)
  @stack = [mustache]
  @partial_template_cache = {}
end

def mustache_in_stack

Returns:
  • (Mustache) - First Mustache in the stack.
def mustache_in_stack
  @mustache_in_stack ||= @stack.find { |frame| frame.is_a?(Mustache) }
end

def partial(name, indentation = '')


to `partial`, we call it and render the result.
representing your profile page or some other template) responds
If the Mustache view handling the rendering (e.g. the view

`partial` method, which would be this sucker right here.
A {{>partial}} tag translates into a call to the context's
def partial(name, indentation = '')
  # Look for the first Mustache in the stack.
  mustache = mustache_in_stack
  # Indent the partial template by the given indentation.
  part = mustache.partial(name).to_s.gsub(/^/, indentation)
  # Get a template object for the partial and render the result.
  template_for_partial(part).render(self)
end

def pop

Returns:
  • (Context) - Returns the Context.
def pop
  @stack.shift
  @mustache_in_stack = nil
  self
end

def push(new_obj)

Returns:
  • (Context) - Returns the Context.

Parameters:
  • new_obj (Object) -- Object to be added to the internal stack.
def push(new_obj)
  @stack.unshift(new_obj)
  @mustache_in_stack = nil
  self
end

def template_for_partial(partial)

def template_for_partial(partial)
  @partial_template_cache[partial] ||= Template.new(partial)
end