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)
def self.classify(underscored) underscored.split(/[-_]/).map do |part| part[0] = part[0].chr.upcase; part end.join end
def self.compiled?
Has this template already been compiled? Compilation is somewhat
def self.compiled? @template.is_a? Template end
def self.path
def self.path template_path end
def self.path=(path)
def self.path=(path) self.template_path = path end
def self.render(*args)
def self.render(*args) new.render(*args) end
def self.template
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
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
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
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)
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)
def self.to_html(*args) render(*args) end
def self.to_text(*args)
def self.to_text(*args) render(*args) end
def self.underscore(classified = 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.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?
def compiled? (@template && @template.is_a?(Template)) || self.class.compiled? end
def context
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 = {})
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
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