class Mustache::Context

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

Public Class Methods

new(mustache) click to toggle source

Expect to be passed an instance of `Mustache`.

# File lib/mustache/context.rb, line 16
def initialize(mustache)
  @stack = [mustache]
end

Public Instance Methods

[](name) click to toggle source

Alias for `fetch`.

# File lib/mustache/context.rb, line 76
def [](name)
  fetch(name, nil)
end
[]=(name, value) click to toggle source

Can be used to add a value to the context in a hash-like way.

context = "Chris"

# File lib/mustache/context.rb, line 71
def []=(name, value)
  push(name => value)
end
escapeHTML(str) click to toggle source

Allows customization of how Mustache escapes things.

Returns a String.

# File lib/mustache/context.rb, line 46
def escapeHTML(str)
  mustache_in_stack.escapeHTML(str)
end
fetch(name, default = :__raise) click to toggle source

Similar to Hash#fetch, finds a value by `name` in the context’s stack. You may specify the default return value by passing a second parameter.

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

# File lib/mustache/context.rb, line 94
def fetch(name, default = :__raise)
  @stack.each do |frame|
    # Prevent infinite recursion.
    next if frame == self

    value = find(frame, name, :__missing)
    if value != :__missing
      return value
    end
  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
find(obj, key, default = nil) click to toggle source

Finds a key in an object, using whatever method is most appropriate. If the object is a hash, does a simple hash lookup. If it’s an object that responds to the key as a method call, invokes that method. You get the idea.

obj - The object to perform the lookup on.
key - The key whose value you want.

default - An optional default value, to return if the

key is not found.

Returns the value of key in obj if it is found and default otherwise.

# File lib/mustache/context.rb, line 123
def find(obj, key, default = nil)
  hash = obj.respond_to?(:has_key?)

  if hash && obj.has_key?(key)
    obj[key]
  elsif hash && obj.has_key?(key.to_s)
    obj[key.to_s]
  elsif !hash && obj.respond_to?(key)
    meth = obj.method(key) rescue proc { obj.send(key) }
    if meth.arity == 1
      meth.to_proc
    else
      meth[]
    end
  else
    default
  end
end
has_key?(key) click to toggle source

Do we know about a particular key? In other words, will calling `context` give us a result that was set. Basically.

# File lib/mustache/context.rb, line 82
def has_key?(key)
  !!fetch(key)
rescue ContextMiss
  false
end
mustache_in_stack() click to toggle source

Find the first Mustache in the stack. If we’re being rendered inside a Mustache object as a context, we’ll use that one.

# File lib/mustache/context.rb, line 39
def mustache_in_stack
  @stack.detect { |frame| frame.is_a?(Mustache) }
end
partial(name, indentation = '') click to toggle source

A {{>partial}} tag translates into a call to the context’s `partial` method, which would be this sucker right here.

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

# File lib/mustache/context.rb, line 26
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(%r^/, indentation)

  # Call the Mustache's `partial` method and render the result.
  result = mustache.render(part, self)
end
pop() click to toggle source

Removes the most recently added object from the context’s internal stack.

Returns the Context.

# File lib/mustache/context.rb, line 63
def pop
  @stack.shift
  self
end
push(new) click to toggle source

Adds a new object to the context’s internal stack.

Returns the Context.

# File lib/mustache/context.rb, line 53
def push(new)
  @stack.unshift(new)
  self
end
Also aliased as: update
update(new) click to toggle source
Alias for: push