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 are unused for now but may become useful in the future.



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

def initialize(options = {})
  @options = options
end

Instance Method Details

#compile(exp) ⇒ Object

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



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

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