class Mustache

view.render # => Hi, mom!<br>view = ‘Mom’
view.template = “Hi, {{person}}!”
view = Mustache.new
The ‘template` setting is also available on instances.
=> “Hello World!”
>> Mustache.render(“Hello {{planet}}”, :planet => “World!”)
cases you may set the `template` setting. For example:
Sometimes you want Mustache to render a string, not a file. In those
* template
setting. It can be a relative or absolute path.
You can tell Mustache exactly which template to us with this
* template_file
for template files. By default it is “html”
The `template_extension` is the extension Mustache uses when looking
* template_extension
load /usr/local/templates/stats.html
other settings are default) a Mustache subclass `Stats` will try to
Setting it to /usr/local/templates, for example, means (given all
looking for a template. By default it is “.”
The `template_path` setting determines the path Mustache uses when
* template_path
Here are the available options:
Mustache uses to find templates.
`Stats.template_path = “/usr/local/templates”` to specify the path
For example, going with the above example, we can use
All settings can be overriden at the class level.
you, this process is completely customizable using a few options.
While Mustache will do its best to load and render a template for
You can skip the instantiation by calling `Stats.render` directly.
* Render that instance: view.render
* Instantiate an instance: view = Stats.new
* Create a template: stats.html
* Create a Mustache subclass: class Stats < Mustache
The typical Mustache workflow is as follows:
should inherit (though it can be used on its own).
Mustache is the base class from which your Mustache subclasses

def self.classify(underscored)

template_partial => TemplatePartial
def self.classify(underscored)
  underscored.split(/[-_]/).map do |part|
    part[0] = part[0].chr.upcase; part
  end.join
end

def self.compiled?

expensive so it may be useful to check this before attempting it.
Has this template already been compiled? Compilation is somewhat
def self.compiled?
  @template.is_a? Template
end

def self.path

Alias for `template_path`
def self.path
  template_path
end

def self.path=(path)

Alias for `template_path`
def self.path=(path)
  self.template_path = path
end

def self.render(*args)

Helper method for quickly instantiating and rendering a view.
def self.render(*args)
  new.render(*args)
end

def self.template

`template=` with a string.
Mustache::Template object here, but you can still safely use
There is a bit of magic here: what we get back is actually a
The template is the actual string Mustache uses as its template.
def self.template
  @template ||= templateify(File.read(template_file))
end

def self.template=(template)

def self.template=(template)
  @template = templateify(template)
end

def self.template_extension

A Mustache template's default extension is 'html'
def self.template_extension
  @template_extension ||= 'html'
end

def self.template_extension=(template_extension)

def self.template_extension=(template_extension)
  @template_extension = template_extension
  @template = nil
end

def self.template_file

use as its template. By default it's ./class_name.html
The template file is the absolute path of the file Mustache will
def self.template_file
  @template_file || "#{path}/#{underscore}.#{template_extension}"
end

def self.template_file=(template_file)

def self.template_file=(template_file)
  @template_file = template_file
  @template = nil
end

def self.template_path

corresponding template. By default it's the current directory (".")
The template path informs your Mustache subclass where to look for its
def self.template_path
  @template_path ||= '.'
end

def self.template_path=(path)

def self.template_path=(path)
  @template_path = File.expand_path(path)
  @template = nil
end

def self.templateify(obj)

returns it.
Turns a string into a Mustache::Template. If passed a Template,
def self.templateify(obj)
  if obj.is_a?(Template)
    obj
  else
    Template.new(obj.to_s, template_path, template_extension)
  end
end

def self.to_html(*args)

Alias for `render`
def self.to_html(*args)
  render(*args)
end

def self.to_text(*args)

Alias for `render`
def self.to_text(*args)
  render(*args)
end

def self.underscore(classified = name)

Takes a string but defaults to using the current class' name.
TemplatePartial => template_partial
def self.underscore(classified = name)
  classified = name if classified.to_s.empty?
  classified = superclass.name if classified.to_s.empty?
  string = classified.dup.split('::').last
  string[0] = string[0].chr.downcase
  string.gsub(/[A-Z]/) { |s| "_#{s.downcase}"}
end

def [](key)

view.render # => "Hi, Jon!"
view.template = "Hi, {{name}}!"
view[:name] = "Jon"
view = Mustache.new

Context accessors.
def [](key)
  context[key.to_sym]
end

def []=(key, value)

def []=(key, value)
  context[key.to_sym] = value
end

def compiled?

Has this instance or its class already compiled a template?
def compiled?
  (@template && @template.is_a?(Template)) || self.class.compiled?
end

def context

and want access to the hash currently being iterated over.
Kind of a hack for now, but useful when you're in an iterating section
A helper method which gives access to the context at a given time.
def context
  @context ||= Context.new(self)
end

def render(data = template, ctx = {})

all special {{tags}} and {{#sections}}replaced{{/sections}}.
Parses our fancy pants template file and returns normal file with
def render(data = template, ctx = {})
  templateify(data).render(context.update(ctx))
end

def template

The template can be set at the instance level.
def template
  @template ||= self.class.template
end

def template=(template)

def template=(template)
  @template = templateify(template)
end

def templateify(obj)

def templateify(obj)
  self.class.templateify(obj)
end