Class: Mustache::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/mustache/generator.rb

Overview

The Generator is in charge of taking an array of Mustache tokens, usually assembled by the Parser, and generating an interpolatable Ruby string. This string is considered the “compiled” template because at that point we’re relying on Ruby to do the parsing and run our code.

For example, let’s take this template:

Hi {{thing}}!

If we run this through the Parser we’ll get these tokens:

[:multi,
  [:static, "Hi "],
  [:mustache, :etag, "thing"],
  [:static, "!\n"]]

Now let’s hand that to the Generator:

>> puts Mustache::Generator.new.compile(tokens) “Hi #.to_s)!n”

You can see the generated Ruby string for any template with the mustache(1) command line tool:

$ mustache --compile test.mustache
"Hi #{CGI.escapeHTML(ctx[:thing].to_s)}!\n"

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Generator

Options can be used to manipulate the resulting ruby code string behavior.



31
32
33
34
# File 'lib/mustache/generator.rb', line 31

def initialize(options = {})
  @options = options
  @option_static_lambdas = options[:static_lambdas] == true
end

Instance Method Details

#compile(exp) ⇒ Object

Given an array of tokens, returns an interpolatable Ruby string.



37
38
39
# File 'lib/mustache/generator.rb', line 37

def compile(exp)
  "\"#{compile!(exp)}\""
end