class Mustache::Template
>> Mustache.render(template, hash)
You shouldn’t use this class directly, instead:
Ruby.
a Ruby string by transforming Mustache tags into interpolated
The idea is this: when handed a Mustache template, convert it into
a raw string template into something usable.
A Template represents a Mustache template. It compiles and caches
def compile(src = @source)
Does the dirty work of transforming a Mustache template into an
def compile(src = @source) Generator.new.compile(tokens(src)) end
def initialize(source)
Expects a Mustache template as a string along with a template
def initialize(source) @source = source end
def render(context)
the compilation step and run the Ruby version of the template
and from then on it is "compiled". Subsequent calls will skip
The first time a template is rendered, this method is overriden
`context`, which should be a simple hash keyed with symbols.
Renders the `@source` Mustache template using the given
def render(context) # Compile our Mustache template into a Ruby string compiled = "def render(ctx) #{compile} end" # Here we rewrite ourself with the interpolated Ruby version of # our Mustache template so subsequent calls are very fast and # can skip the compilation stage. instance_eval(compiled, __FILE__, __LINE__ - 1) # Call the newly rewritten version of #render render(context) end
def tokens(src = @source)
def tokens(src = @source) Parser.new.compile(src) end