Mustache is the base class from which your Mustache subclasses should inherit (though it can be used on its own).

The typical Mustache workflow is as follows:

  • Create a Mustache subclass: class Stats < Mustache
  • Create a template: stats.mustache
  • Instantiate an instance: view = Stats.new
  • Render that instance: view.render

You can skip the instantiation by calling `Stats.render` directly.

While Mustache will do its best to load and render a template for you, this process is completely customizable using a few options.

All settings can be overriden at the class level.

For example, going with the above example, we can use `Stats.template_path = “/usr/local/templates“` to specify the path Mustache uses to find templates.

Here are the available options:

The `template_path` setting determines the path Mustache uses when looking for a template. By default it is “.” Setting it to /usr/local/templates, for example, means (given all other settings are default) a Mustache subclass `Stats` will try to load /usr/local/templates/stats.mustache

The `template_extension` is the extension Mustache uses when looking for template files. By default it is “mustache“

You can tell Mustache exactly which template to us with this setting. It can be a relative or absolute path.

  • template

Sometimes you want Mustache to render a string, not a file. In those cases you may set the `template` setting. For example:

  >> Mustache.render("Hello {{planet}}", :planet => "World!")
  => "Hello World!"

The `template` setting is also available on instances.

  view = Mustache.new
  view.template = "Hi, {{person}}!"
  view[:person] = 'Mom'
  view.render # => Hi, mom!

To make life easy on those developing Mustache plugins for web frameworks or other libraries, Mustache will attempt to load view classes (i.e. Mustache subclasses) using the `view_class` class method. The `view_namespace` tells Mustache under which constant view classes live. By default it is `Object`.

Similar to `template_path`, the `view_path` option tells Mustache where to look for files containing view classes when using the `view_class` method.

Methods
#
C
P
R
T
U
V
Classes and Modules
Constants
Version = '0.10.0'
Attributes
[W] raise_on_context_miss
Class Public methods
classify(underscored)

template_partial => TemplatePartial

# File lib/mustache.rb, line 264
  def self.classify(underscored)
    underscored.split(/[-_]/).map do |part|
      part[0] = part[0].chr.upcase; part
    end.join
  end
compiled?()

Has this template already been compiled? Compilation is somewhat expensive so it may be useful to check this before attempting it.

# File lib/mustache.rb, line 254
  def self.compiled?
    @template.is_a? Template
  end
partial(name)

Given a name, attempts to read a file and return the contents as a string. The file is not rendered, so it might contain {{mustaches}}.

Call `render` if you need to process it.

# File lib/mustache.rb, line 105
  def self.partial(name)
    File.read("#{template_path}/#{name}.#{template_extension}")
  end
path()

Alias for `template_path`

# File lib/mustache.rb, line 128
  def self.path
    template_path
  end
path=(path)

Alias for `template_path`

# File lib/mustache.rb, line 133
  def self.path=(path)
    self.template_path = path
  end
raise_on_context_miss=(boolean)
# File lib/mustache.rb, line 248
  def self.raise_on_context_miss=(boolean)
    @raise_on_context_miss = boolean
  end
raise_on_context_miss?()

Should an exception be raised when we cannot find a corresponding method or key in the current context? By default this is false to emulate ctemplate’s behavior, but it may be useful to enable when debugging or developing.

If set to true and there is a context miss, `Mustache::ContextMiss` will be raised.

# File lib/mustache.rb, line 244
  def self.raise_on_context_miss?
    @raise_on_context_miss
  end
render(*args)

Helper method for quickly instantiating and rendering a view.

# File lib/mustache.rb, line 74
  def self.render(*args)
    new.render(*args)
  end
render_file(name, context = {})

Given a file name and an optional context, attempts to load and render the file as a template.

# File lib/mustache.rb, line 90
  def self.render_file(name, context = {})
    render(partial(name), context)
  end
template()

The template is the actual string Mustache uses as its template. There is a bit of magic here: what we get back is actually a Mustache::Template object here, but you can still safely use `template=` with a string.

# File lib/mustache.rb, line 173
  def self.template
    @template ||= templateify(File.read(template_file))
  end
template=(template)
# File lib/mustache.rb, line 177
  def self.template=(template)
    @template = templateify(template)
  end
template_extension()

A Mustache template’s default extension is ‘mustache’

# File lib/mustache.rb, line 138
  def self.template_extension
    @template_extension ||= 'mustache'
  end
template_extension=(template_extension)
# File lib/mustache.rb, line 142
  def self.template_extension=(template_extension)
    @template_extension = template_extension
    @template = nil
  end
template_file()

The template file is the absolute path of the file Mustache will use as its template. By default it’s ./class_name.mustache

# File lib/mustache.rb, line 160
  def self.template_file
    @template_file || "#{path}/#{template_name}.#{template_extension}"
  end
template_file=(template_file)
# File lib/mustache.rb, line 164
  def self.template_file=(template_file)
    @template_file = template_file
    @template = nil
  end
template_name()

The template name is the Mustache template file without any extension or other information. Defaults to `class_name`.

# File lib/mustache.rb, line 149
  def self.template_name
    @template_name || underscore
  end
template_name=(template_name)
# File lib/mustache.rb, line 153
  def self.template_name=(template_name)
    @template_name = template_name
    @template = nil
  end
template_path()

The template path informs your Mustache subclass where to look for its corresponding template. By default it’s the current directory (“.”)

# File lib/mustache.rb, line 118
  def self.template_path
    @template_path ||= '.'
  end
template_path=(path)
# File lib/mustache.rb, line 122
  def self.template_path=(path)
    @template_path = File.expand_path(path)
    @template = nil
  end
templateify(obj)

Turns a string into a Mustache::Template. If passed a Template, returns it.

# File lib/mustache.rb, line 283
  def self.templateify(obj)
    if obj.is_a?(Template)
      obj
    else
      Template.new(obj.to_s)
    end
  end
to_html(*args)

Alias for `render`

# File lib/mustache.rb, line 79
  def self.to_html(*args)
    render(*args)
  end
to_text(*args)

Alias for `render`

# File lib/mustache.rb, line 84
  def self.to_text(*args)
    render(*args)
  end
underscore(classified = name)

TemplatePartial => template_partial Takes a string but defaults to using the current class’ name.

# File lib/mustache.rb, line 272
  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
view_class(name)

When given a symbol or string representing a class, will try to produce an appropriate view class. e.g.

  Mustache.view_namespace = Hurl::Views
  Mustache.view_class(:Partial) # => Hurl::Views::Partial
# File lib/mustache.rb, line 207
  def self.view_class(name)
    if name != classify(name.to_s)
      name = classify(name.to_s)
    end

    # Emptiness begets emptiness.
    if name.to_s == ''
      return Mustache
    end

    file_name = underscore(name)
    namespace = view_namespace

    if namespace.const_defined?(:Views) && namespace::Views.const_defined?(name)
      namespace::Views.const_get(name)
    elsif namespace.const_defined?(name)
      namespace.const_get(name)
    elsif File.exists?(file = "#{view_path}/#{file_name}.rb")
      require "#{file}".chomp('.rb')
      if namespace.const_defined?(:Views)
        namespace::Views.const_get(name)
      else
        namespace.const_get(name)
      end
    else
      Mustache
    end
  rescue NameError
    Mustache
  end
view_namespace()

The constant under which Mustache will look for views. By default it’s `Object`, but it might be nice to set it to something like `Hurl::Views` if your app’s main namespace is `Hurl`.

# File lib/mustache.rb, line 184
  def self.view_namespace
    @view_namespace || Object
  end
view_namespace=(namespace)
# File lib/mustache.rb, line 188
  def self.view_namespace=(namespace)
    @view_namespace = namespace
  end
view_path()

Mustache searches the view path for .rb files to require when asked to find a view class. Defaults to “.“

# File lib/mustache.rb, line 194
  def self.view_path
    @view_path ||= '.'
  end
view_path=(path)
# File lib/mustache.rb, line 198
  def self.view_path=(path)
    @view_path = path
  end
Instance Public methods
[](key)

Context accessors.

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

# File lib/mustache.rb, line 323
  def [](key)
    context[key.to_sym]
  end
[]=(key, value)
# File lib/mustache.rb, line 327
  def []=(key, value)
    context[key.to_sym] = value
  end
compiled?()

Has this instance or its class already compiled a template?

# File lib/mustache.rb, line 259
  def compiled?
    (@template && @template.is_a?(Template)) || self.class.compiled?
  end
context()

A helper method which gives access to the context at a given time. Kind of a hack for now, but useful when you’re in an iterating section and want access to the hash currently being iterated over.

# File lib/mustache.rb, line 313
  def context
    @context ||= Context.new(self)
  end
partial(name)

Override this in your subclass if you want to do fun things like reading templates from a database. It will be rendered by the context, so all you need to do is return a string.

# File lib/mustache.rb, line 112
  def partial(name)
    self.class.partial(name)
  end
raise_on_context_miss?()

Instance level version of `Mustache.raise_on_context_miss?`

# File lib/mustache.rb, line 305
  def raise_on_context_miss?
    self.class.raise_on_context_miss? || @raise_on_context_miss
  end
render(data = template, ctx = {})

Parses our fancy pants template file and returns normal file with all special {{tags}} and {{sections}}replaced{{/sections}}.

This method is also aliased as to_html to_text
# File lib/mustache.rb, line 333
  def render(data = template, ctx = {})
    tpl = templateify(data)

    return tpl.render(context) if ctx == {}

    begin
      context.push(ctx)
      tpl.render(context)
    ensure
      context.pop
    end
  end
render_file(name, context = {})

Given a file name and an optional context, attempts to load and render the file as a template.

# File lib/mustache.rb, line 96
  def render_file(name, context = {})
    self.class.render_file(name, context)
  end
template()

The template can be set at the instance level.

# File lib/mustache.rb, line 296
  def template
    @template ||= self.class.template
  end
template=(template)
# File lib/mustache.rb, line 300
  def template=(template)
    @template = templateify(template)
  end
templateify(obj)
# File lib/mustache.rb, line 291
  def templateify(obj)
    self.class.templateify(obj)
  end
to_html(data = template, ctx = {})

Alias for render

to_text(data = template, ctx = {})

Alias for render