Class Mustache
In: lib/mustache/generator.rb
lib/mustache/sinatra.rb
lib/mustache/version.rb
lib/mustache/template.rb
lib/mustache/parser.rb
lib/mustache/context.rb
lib/mustache.rb
Parent: Object

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.

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

Classes and Modules

Module Mustache::Sinatra
Class Mustache::Context
Class Mustache::ContextMiss
Class Mustache::Generator
Class Mustache::Parser
Class Mustache::Template

Constants

Version = VERSION = '0.11.2'

Attributes

raise_on_context_miss  [W] 

Public Class methods

template_partial => TemplatePartial

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

Return the value of the configuration setting on the superclass, or return the default.

attr_name - Symbol name of the attribute. It should match the instance variable. default - Default value to use if the superclass does not respond.

Returns the inherited or default configuration setting.

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.

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.

Helper method for quickly instantiating and rendering a view.

Given a file name and an optional context, attempts to load and render the file as a 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.

A Mustache template‘s default extension is ‘mustache‘

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

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

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

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

TemplatePartial => template_partial Takes a string but defaults to using the current 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

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`.

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

Public Instance methods

Context accessors.

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

Has this instance or its class already compiled a template?

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.

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.

Instance level version of `Mustache.raise_on_context_miss?`

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

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

The template can be set at the instance level.

to_html(data = template, ctx = {})

Alias for render

to_text(data = template, ctx = {})

Alias for render

[Validate]