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
Initializes a Mustache::Context.
@param [Mustache] mustache A Mustache instance.
# File lib/mustache/context.rb, line 14 def initialize(mustache) @stack = [mustache] @partial_template_cache = {} end
Public Instance Methods
Alias for `fetch`.
# File lib/mustache/context.rb, line 93 def [](name) fetch(name, nil) end
Can be used to add a value to the context in a hash-like way.
context = “Chris”
# File lib/mustache/context.rb, line 88 def []=(name, value) push(name => value) end
# File lib/mustache/context.rb, line 148 def current @stack.first end
Allows customization of how Mustache escapes things.
@param [String] str String to escape.
@return [String] Escaped HTML string.
# File lib/mustache/context.rb, line 58 def escapeHTML(str) mustache_in_stack.escapeHTML(str) end
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 111 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
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.
@param [Object] obj The object to perform the lookup on. @param [String,Symbol] key The key whose value you want @param [Object] default An optional default value, to return if the key is not found.
@return [Object] The value of key in object if it is found, and default otherwise.
# File lib/mustache/context.rb, line 138 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
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 99 def has_key?(key) !!fetch(key, false) rescue ContextMiss false end
Find the first Mustache in the stack.
If we're being rendered inside a Mustache object as a context, we'll use that one.
@return [Mustache] First Mustache in the stack.
# File lib/mustache/context.rb, line 48 def mustache_in_stack @mustache_in_stack ||= @stack.find { |frame| frame.is_a?(Mustache) } end
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(/^/, indentation) # Get a template object for the partial and render the result. template_for_partial(part).render(self) end
Removes the most recently added object from the context's internal stack.
@return [Context] Returns the Context.
# File lib/mustache/context.rb, line 79 def pop @stack.shift @mustache_in_stack = nil self end
Adds a new object to the context's internal stack.
@param [Object] new_obj Object to be added to the internal stack.
@return [Context] Returns the Context.
# File lib/mustache/context.rb, line 68 def push(new_obj) @stack.unshift(new_obj) @mustache_in_stack = nil self end
# File lib/mustache/context.rb, line 37 def template_for_partial(partial) @partial_template_cache[partial] ||= Template.new(partial) end
Private Instance Methods
Fetches a hash key if it exists, or returns the given default.
# File lib/mustache/context.rb, line 162 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) obj.fetch(key, default) end
If a class, we need to find tags (methods) per Parser::ALLOWED_CONTENT.
# File lib/mustache/context.rb, line 157 def to_tag key key.to_s.include?('-') ? key.to_s.tr('-', '_') : key end