Class: Mustache::Template
- Inherits:
-
Object
- Object
- Mustache::Template
- Defined in:
- lib/mustache/template.rb
Overview
A Template represents a Mustache template. It compiles and caches a raw string template into something usable.
The idea is this: when handed a Mustache template, convert it into a Ruby string by transforming Mustache tags into interpolated Ruby.
You shouldn’t use this class directly, instead:
>> Mustache.render(template, hash)
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
Class Method Summary collapse
-
.recursor(toks, section, &block) ⇒ Object
Simple recursive iterator for tokens.
Instance Method Summary collapse
-
#compile(src = @source) ⇒ Object
(also: #to_s)
Does the dirty work of transforming a Mustache template into an interpolation-friendly Ruby string.
-
#initialize(source, options = {}) ⇒ Template
constructor
Expects a Mustache template as a string along with a template path, which it uses to find partials.
-
#partials ⇒ Array
Returns an array of partials.
-
#render(context) ⇒ Object
Renders the ‘@source` Mustache template using the given `context`, which should be a simple hash keyed with symbols.
-
#sections ⇒ Array
Returns an array of sections.
-
#tags ⇒ Array
Returns an array of tags.
-
#tokens(src = @source) ⇒ Array
Returns an array of tokens for a given template.
Constructor Details
#initialize(source, options = {}) ⇒ Template
Expects a Mustache template as a string along with a template path, which it uses to find partials. Options may be passed.
22 23 24 25 |
# File 'lib/mustache/template.rb', line 22 def initialize(source, = {}) @source = source @options = end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
18 19 20 |
# File 'lib/mustache/template.rb', line 18 def source @source end |
Class Method Details
.recursor(toks, section, &block) ⇒ Object
Simple recursive iterator for tokens
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/mustache/template.rb', line 115 def self.recursor(toks, section, &block) toks.map do |token| next unless token.is_a? Array if token.first == :mustache new_token, new_section, result, stop = yield(token, section) [ result ] + ( stop ? [] : recursor(new_token, new_section, &block)) else recursor(token, section, &block) end end end |
Instance Method Details
#compile(src = @source) ⇒ Object Also known as: to_s
Does the dirty work of transforming a Mustache template into an interpolation-friendly Ruby string.
49 50 51 |
# File 'lib/mustache/template.rb', line 49 def compile(src = @source) Generator.new(@options).compile(tokens(src)) end |
#partials ⇒ Array
Returns an array of partials.
Partials that belong to sections are included, but the section name is not preserved
103 104 105 106 107 108 109 110 111 |
# File 'lib/mustache/template.rb', line 103 def partials Template.recursor(tokens, []) do |token, section| if token[1] == :partial [ new_token=token, new_section=section, result=token[2], stop=true ] else [ new_token=token, new_section=section, result=nil, stop=false ] end end.flatten.reject(&:nil?).uniq end |
#render(context) ⇒ Object
Renders the ‘@source` Mustache template using the given `context`, which should be a simple hash keyed with symbols.
The first time a template is rendered, this method is overriden and from then on it is “compiled”. Subsequent calls will skip the compilation step and run the Ruby version of the template directly.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mustache/template.rb', line 34 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 |
#sections ⇒ Array
Returns an array of sections.
Sections that belong to other sections will be of the form ‘section1.childsection`
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/mustache/template.rb', line 86 def sections Template.recursor(tokens, []) do |token, section| if [:section, :inverted_section].include?(token[1]) new_section=(section + [token[2][2][0]]) [ new_token=token[4], new_section, result=new_section.join('.'), stop=false ] else [ new_token=token, new_section=section, result=nil, stop=false ] end end.flatten.reject(&:nil?).uniq end |
#tags ⇒ Array
Returns an array of tags.
Tags that belong to sections will be of the form ‘section1.tag`.
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/mustache/template.rb', line 68 def Template.recursor(tokens, []) do |token, section| if [:etag, :utag].include?(token[1]) [ new_token=nil, new_section=nil, result=((section + [token[2][2][0]]).join('.')), stop=true ] elsif [:section, :inverted_section].include?(token[1]) [ new_token=token[4], new_section=(section + [token[2][2][0]]), result=nil, stop=false ] else [ new_token=token, new_section=section, result=nil, stop=false ] end end.flatten.reject(&:nil?).uniq end |