Class: Mustache::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/mustache/context.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(mustache) ⇒ Context

Initializes a Mustache::Context.

Parameters:

  • mustache (Mustache)

    A Mustache instance.


14
15
16
# File 'lib/mustache/context.rb', line 14

def initialize(mustache)
  @stack = [mustache]
end

Instance Method Details

#[](name) ⇒ Object

Alias for `fetch`.


88
89
90
# File 'lib/mustache/context.rb', line 88

def [](name)
  fetch(name, nil)
end

#[]=(name, value) ⇒ Object

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

context = “Chris”


83
84
85
# File 'lib/mustache/context.rb', line 83

def []=(name, value)
  push(name => value)
end

#currentObject


143
144
145
# File 'lib/mustache/context.rb', line 143

def current
  @stack.first
end

#escapeHTML(str) ⇒ String

Allows customization of how Mustache escapes things.

Parameters:

  • str (String)

    String to escape.

Returns:

  • (String)

    Escaped HTML string.


53
54
55
# File 'lib/mustache/context.rb', line 53

def escapeHTML(str)
  mustache_in_stack.escapeHTML(str)
end

#fetch(name, default = :__raise) ⇒ Object

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.


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mustache/context.rb', line 106

def fetch(name, default = :__raise)
  @stack.each do |frame|
    # Prevent infinite recursion.
    next if frame == self

    value = find(frame, name, :__missing)
    return value if value != :__missing
  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) ⇒ Object

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.

Parameters:

  • obj (Object)

    The object to perform the lookup on.

  • key (String, Symbol)

    The key whose value you want

  • default (Object) (defaults to: nil)

    An optional default value, to return if the key is not found.

Returns:

  • (Object)

    The value of key in object if it is found, and default otherwise.


133
134
135
136
137
138
139
140
141
# File 'lib/mustache/context.rb', line 133

def find(obj, key, default = nil)
  return find_in_hash(obj.to_hash, key, default) if obj.respond_to?(:to_hash)

  key = to_tag(key)
  return default unless obj.respond_to?(key)

  meth = obj.method(key) rescue proc { obj.send(key) }
  meth.arity == 1 ? meth.to_proc : meth.call
end

#has_key?(key) ⇒ Boolean

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

Returns:

  • (Boolean)

94
95
96
97
98
# File 'lib/mustache/context.rb', line 94

def has_key?(key)
  !!fetch(key, false)
rescue ContextMiss
  false
end

#mustache_in_stackMustache

Find the first Mustache in the stack.

If we're being rendered inside a Mustache object as a context, we'll use that one.

Returns:

  • (Mustache)

    First Mustache in the stack.


43
44
45
# File 'lib/mustache/context.rb', line 43

def mustache_in_stack
  @mustache_in_stack ||= @stack.find { |frame| frame.is_a?(Mustache) }
end

#partial(name, indentation = '') ⇒ Object

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.


25
26
27
28
29
30
31
32
33
34
# File 'lib/mustache/context.rb', line 25

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)

  # Call the Mustache's `partial` method and render the result.
  mustache.render(part, self)
end

#popContext

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

Returns:

  • (Context)

    Returns the Context.


74
75
76
77
78
# File 'lib/mustache/context.rb', line 74

def pop
  @stack.shift
  @mustache_in_stack = nil
  self
end

#push(new_obj) ⇒ Context

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

Parameters:

  • new_obj (Object)

    Object to be added to the internal stack.

Returns:

  • (Context)

    Returns the Context.


63
64
65
66
67
# File 'lib/mustache/context.rb', line 63

def push(new_obj)
  @stack.unshift(new_obj)
  @mustache_in_stack = nil
  self
end