class Mustache::Generator
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 #{CGI.escapeHTML(ctx.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"
Public Class Methods
Source
# File lib/mustache/generator.rb, line 31 def initialize(options = {}) @options = options @option_static_lambdas = options[:static_lambdas] == true end
Options can be used to manipulate the resulting ruby code string behavior.
Public Instance Methods
Source
# File lib/mustache/generator.rb, line 37 def compile(exp) "\"#{compile!(exp)}\"" end
Given an array of tokens, returns an interpolatable Ruby string.