Class: Mustache
- Inherits:
-
Object
- Object
- Mustache
- Defined in:
- lib/mustache.rb,
lib/mustache/parser.rb,
lib/mustache/context.rb,
lib/mustache/version.rb,
lib/mustache/sinatra.rb,
lib/mustache/settings.rb,
lib/mustache/template.rb,
lib/mustache/generator.rb
Overview
Settings which can be configured for all view classes, a single view class, or a single Mustache instance.
Direct Known Subclasses
Defined Under Namespace
Modules: Sinatra Classes: Context, ContextMiss, Generator, Parser, Template
Constant Summary
- Version =
VERSION = '0.99.4'
Class Method Summary (collapse)
-
+ (Object) classify(underscored)
template_partial => TemplatePartial template/partial => Template::Partial.
-
+ (Boolean) compiled?
Has this template already been compiled? Compilation is somewhat expensive so it may be useful to check this before attempting it.
-
+ (Object) const_get!(name)
Supercharged version of Module#const_get.
-
+ (Object) inheritable_config_for(attr_name, default)
Return the value of the configuration setting on the superclass, or return the default.
-
+ (Object) partial(name)
Given a name, attempts to read a file and return the contents as a string.
-
+ (Object) path
Alias for `template_path`.
-
+ (Object) path=(path)
Alias for `template_path`.
- + (Object) raise_on_context_miss=(boolean)
-
+ (Boolean) raise_on_context_miss?
Should an exception be raised when we cannot find a corresponding method or key in the current context? By default this is false to emulate ctemplate's behavior, but it may be useful to enable when debugging or developing.
-
+ (Object) render(*args)
Instantiates an instance of this class and calls `render` with the passed args.
-
+ (Object) render_file(name, context = {})
Given a file name and an optional context, attempts to load and render the file as a template.
-
+ (Object) template
The template is the actual string Mustache uses as its template.
- + (Object) template=(template)
-
+ (Object) template_extension
A Mustache template's default extension is 'mustache', but this can be changed.
- + (Object) template_extension=(template_extension)
-
+ (Object) template_file
The template file is the absolute path of the file Mustache will use as its template.
- + (Object) template_file=(template_file)
-
+ (Object) template_name
The template name is the Mustache template file without any extension or other information.
- + (Object) template_name=(template_name)
-
+ (Object) template_path
The template path informs your Mustache view where to look for its corresponding template.
- + (Object) template_path=(path)
-
+ (Object) templateify(obj)
Turns a string into a Mustache::Template.
-
+ (Object) to_html
Instantiates an instance of this class and calls `render` with the passed args.
-
+ (Object) to_text
Instantiates an instance of this class and calls `render` with the passed args.
-
+ (Object) underscore(classified = name)
TemplatePartial => template_partial Template::Partial => template/partial Takes a string but defaults to using the current class' name.
-
+ (Object) view_class(name)
When given a symbol or string representing a class, will try to produce an appropriate view class.
-
+ (Object) view_namespace
The constant under which Mustache will look for views when autoloading.
- + (Object) view_namespace=(namespace)
-
+ (Object) view_path
Mustache searches the view path for .rb files to require when asked to find a view class.
- + (Object) view_path=(path)
Instance Method Summary (collapse)
-
- (Object) [](key)
Context accessors.
- - (Object) []=(key, value)
-
- (Boolean) compiled?
Has this instance or its class already compiled a template?.
-
- (Object) context
A helper method which gives access to the context at a given time.
-
- (Object) escapeHTML(str)
Override this to provide custom escaping.
-
- (Object) partial(name)
Override this in your subclass if you want to do fun things like reading templates from a database.
- - (Object) raise_on_context_miss=(boolean)
-
- (Boolean) raise_on_context_miss?
Instance level version of `Mustache.raise_on_context_miss?`.
-
- (Object) render(data = template, ctx = {})
(also: #to_html, #to_text)
Parses our fancy pants template file and returns normal file with all special {tags} and {{#sections}replaced{/sections}.
-
- (Object) render_file(name, context = {})
Given a file name and an optional context, attempts to load and render the file as a template.
-
- (Object) template
The template can be set at the instance level.
- - (Object) template=(template)
- - (Object) template_extension
- - (Object) template_extension=(template_extension)
-
- (Object) template_file
The template file is the absolute path of the file Mustache will use as its template.
- - (Object) template_file=(template_file)
- - (Object) template_name
- - (Object) template_name=(template_name)
- - (Object) template_path (also: #path)
- - (Object) template_path=(path) (also: #path=)
- - (Object) templateify(obj)
Class Method Details
+ (Object) classify(underscored)
template_partial => TemplatePartial template/partial => Template::Partial
257 258 259 260 261 262 263 |
# File 'lib/mustache.rb', line 257 def self.classify(underscored) underscored.split('/').map do |namespace| namespace.split(/[-_]/).map do |part| part[0] = part[0].chr.upcase; part end.join end.join('::') end |
+ (Boolean) compiled?
Has this template already been compiled? Compilation is somewhat expensive so it may be useful to check this before attempting it.
246 247 248 |
# File 'lib/mustache.rb', line 246 def self.compiled? @template.is_a? Template end |
+ (Object) const_get!(name)
Supercharged version of Module#const_get.
Always searches under Object and can find constants by their full name,
e.g. Mustache::Views::Index
name - The full constant name to find.
Returns the constant if found Returns nil if nothing is found
236 237 238 239 240 241 242 |
# File 'lib/mustache.rb', line 236 def self.const_get!(name) name.split('::').inject(Object) do |klass, cname| klass.const_get(cname) end rescue NameError nil end |
+ (Object) inheritable_config_for(attr_name, default)
Return the value of the configuration setting on the superclass, or return the default.
attr_name - Symbol name of the attribute. It should match the instance variable. default - Default value to use if the superclass does not respond.
Returns the inherited or default configuration setting.
301 302 303 |
# File 'lib/mustache.rb', line 301 def self.inheritable_config_for(attr_name, default) superclass.respond_to?(attr_name) ? superclass.send(attr_name) : default end |
+ (Object) partial(name)
Given a name, attempts to read a file and return the contents as a string. The file is not rendered, so it might contain {mustaches}.
Call `render` if you need to process it.
170 171 172 |
# File 'lib/mustache.rb', line 170 def self.partial(name) File.read("#{template_path}/#{name}.#{template_extension}") end |
+ (Object) path
Alias for `template_path`
34 35 36 |
# File 'lib/mustache/settings.rb', line 34 def self.path template_path end |
+ (Object) path=(path)
Alias for `template_path`
40 41 42 |
# File 'lib/mustache/settings.rb', line 40 def self.path=(path) self.template_path = path end |
+ (Object) raise_on_context_miss=(boolean)
181 182 183 |
# File 'lib/mustache/settings.rb', line 181 def self.raise_on_context_miss=(boolean) @raise_on_context_miss = boolean end |
+ (Boolean) raise_on_context_miss?
Should an exception be raised when we cannot find a corresponding method or key in the current context? By default this is false to emulate ctemplate's behavior, but it may be useful to enable when debugging or developing.
If set to true and there is a context miss, `Mustache::ContextMiss` will be raised.
177 178 179 |
# File 'lib/mustache/settings.rb', line 177 def self.raise_on_context_miss? @raise_on_context_miss end |
+ (Object) render(*args)
Instantiates an instance of this class and calls `render` with the passed args.
Returns a rendered String version of a template
83 84 85 |
# File 'lib/mustache.rb', line 83 def self.render(*args) new.render(*args) end |
+ (Object) render_file(name, context = {})
Given a file name and an optional context, attempts to load and render the file as a template.
155 156 157 |
# File 'lib/mustache.rb', line 155 def self.render_file(name, context = {}) render(partial(name), context) end |
+ (Object) template
The template is the actual string Mustache uses as its template. There is a bit of magic here: what we get back is actually a Mustache::Template object, but you can still safely use `template=`
with a string.
141 142 143 |
# File 'lib/mustache/settings.rb', line 141 def self.template @template ||= templateify(File.read(template_file)) end |
+ (Object) template=(template)
145 146 147 |
# File 'lib/mustache/settings.rb', line 145 def self.template=(template) @template = templateify(template) end |
+ (Object) template_extension
A Mustache template's default extension is 'mustache', but this can be changed.
52 53 54 |
# File 'lib/mustache/settings.rb', line 52 def self.template_extension @template_extension ||= inheritable_config_for :template_extension, 'mustache' end |
+ (Object) template_extension=(template_extension)
56 57 58 59 |
# File 'lib/mustache/settings.rb', line 56 def self.template_extension=(template_extension) @template_extension = template_extension @template = nil end |
+ (Object) template_file
The template file is the absolute path of the file Mustache will use as its template. By default it's ./class_name.mustache
111 112 113 |
# File 'lib/mustache/settings.rb', line 111 def self.template_file @template_file || "#{path}/#{template_name}.#{template_extension}" end |
+ (Object) template_file=(template_file)
115 116 117 118 |
# File 'lib/mustache/settings.rb', line 115 def self.template_file=(template_file) @template_file = template_file @template = nil end |
+ (Object) template_name
The template name is the Mustache template file without any extension or other information. Defaults to `class_name`.
You may want to change this if your class is named Stat but you want to re-use another template.
class Stat
self.template_name = "graphs" # use graphs.mustache
end
85 86 87 |
# File 'lib/mustache/settings.rb', line 85 def self.template_name @template_name || underscore end |
+ (Object) template_name=(template_name)
89 90 91 92 |
# File 'lib/mustache/settings.rb', line 89 def self.template_name=(template_name) @template_name = template_name @template = nil end |
+ (Object) template_path
The template path informs your Mustache view where to look for its corresponding template. By default it's the current directory (".")
A class named Stat with a template_path of "app/templates" will look for "app/templates/stat.mustache"
15 16 17 |
# File 'lib/mustache/settings.rb', line 15 def self.template_path @template_path ||= inheritable_config_for :template_path, '.' end |
+ (Object) template_path=(path)
19 20 21 22 |
# File 'lib/mustache/settings.rb', line 19 def self.template_path=(path) @template_path = File.(path) @template = nil end |
+ (Object) templateify(obj)
Turns a string into a Mustache::Template. If passed a Template, returns it.
282 283 284 285 286 287 288 |
# File 'lib/mustache.rb', line 282 def self.templateify(obj) if obj.is_a?(Template) obj else Template.new(obj.to_s) end end |
+ (Object) to_html
Instantiates an instance of this class and calls `render` with the passed args.
Returns a rendered String version of a template
88 89 90 |
# File 'lib/mustache.rb', line 88 def self.render(*args) new.render(*args) end |
+ (Object) to_text
Instantiates an instance of this class and calls `render` with the passed args.
Returns a rendered String version of a template
89 90 91 |
# File 'lib/mustache.rb', line 89 def self.render(*args) new.render(*args) end |
+ (Object) underscore(classified = name)
TemplatePartial => template_partial Template::Partial => template/partial Takes a string but defaults to using the current class' name.
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/mustache.rb', line 268 def self.underscore(classified = name) classified = name if classified.to_s.empty? classified = superclass.name if classified.to_s.empty? string = classified.dup.split("#{view_namespace}::").last string.split('::').map do |part| part[0] = part[0].chr.downcase part.gsub(/[A-Z]/) { |s| "_#{s.downcase}"} end.join('/') end |
+ (Object) view_class(name)
When given a symbol or string representing a class, will try to produce an appropriate view class. e.g.
Mustache.view_namespace = Hurl::Views
Mustache.view_class(:Partial) # => Hurl::Views::Partial
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/mustache.rb', line 204 def self.view_class(name) if name != classify(name.to_s) name = classify(name.to_s) end # Emptiness begets emptiness. if name.to_s == '' return Mustache end file_name = underscore(name) name = "#{view_namespace}::#{name}" if const = const_get!(name) const elsif File.exists?(file = "#{view_path}/#{file_name}.rb") require "#{file}".chomp('.rb') const_get!(name) || Mustache else Mustache end end |
+ (Object) view_namespace
The constant under which Mustache will look for views when autoloading. By default the view namespace is `Object`, but it might be nice to set it to something like `Hurl::Views` if your app's main namespace is `Hurl`.
203 204 205 |
# File 'lib/mustache/settings.rb', line 203 def self.view_namespace @view_namespace ||= inheritable_config_for(:view_namespace, Object) end |
+ (Object) view_namespace=(namespace)
207 208 209 |
# File 'lib/mustache/settings.rb', line 207 def self.view_namespace=(namespace) @view_namespace = namespace end |
+ (Object) view_path
Mustache searches the view path for .rb files to require when asked to find a view class. Defaults to "."
219 220 221 |
# File 'lib/mustache/settings.rb', line 219 def self.view_path @view_path ||= inheritable_config_for(:view_path, '.') end |
+ (Object) view_path=(path)
223 224 225 |
# File 'lib/mustache/settings.rb', line 223 def self.view_path=(path) @view_path = path end |
Instance Method Details
- (Object) [](key)
Context accessors.
view = Mustache.new view = "Jon" view.template = "Hi, {name}!" view.render # => "Hi, Jon!"
138 139 140 |
# File 'lib/mustache.rb', line 138 def [](key) context[key.to_sym] end |
- (Object) []=(key, value)
142 143 144 |
# File 'lib/mustache.rb', line 142 def []=(key, value) context[key.to_sym] = value end |
- (Boolean) compiled?
Has this instance or its class already compiled a template?
251 252 253 |
# File 'lib/mustache.rb', line 251 def compiled? (@template && @template.is_a?(Template)) || self.class.compiled? end |
- (Object) context
A helper method which gives access to the context at a given time. Kind of a hack for now, but useful when you're in an iterating section and want access to the hash currently being iterated over.
149 150 151 |
# File 'lib/mustache.rb', line 149 def context @context ||= Context.new(self) end |
- (Object) escapeHTML(str)
Override this to provide custom escaping.
class PersonView < Mustache
def escapeHTML(str)
my_html_escape_method(str)
end
end
Returns a String
190 191 192 |
# File 'lib/mustache.rb', line 190 def escapeHTML(str) CGI.escapeHTML(str) end |
- (Object) partial(name)
Override this in your subclass if you want to do fun things like reading templates from a database. It will be rendered by the context, so all you need to do is return a string.
177 178 179 |
# File 'lib/mustache.rb', line 177 def partial(name) self.class.partial(name) end |
- (Object) raise_on_context_miss=(boolean)
190 191 192 |
# File 'lib/mustache/settings.rb', line 190 def raise_on_context_miss=(boolean) @raise_on_context_miss = boolean end |
- (Boolean) raise_on_context_miss?
Instance level version of `Mustache.raise_on_context_miss?`
186 187 188 |
# File 'lib/mustache/settings.rb', line 186 def raise_on_context_miss? self.class.raise_on_context_miss? || @raise_on_context_miss end |
- (Object) render(data = template, ctx = {}) Also known as: to_html, to_text
Parses our fancy pants template file and returns normal file with all special {tags} and Mustache.{{#sections}replaced{/sections}.
data - A String template or a Hash context. If a Hash is given,
we'll try to figure out the template from the class.
Examples
@view.render("Hi {{thing}}!", :thing => :world)
View.template = "Hi {{thing}}!"
@view = View.new
@view.render(:thing => :world)
Returns a rendered String version of a template
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/mustache.rb', line 108 def render(data = template, ctx = {}) if data.is_a? Hash ctx = data tpl = templateify(template) elsif data.is_a? Symbol self.template_name = data tpl = templateify(template) else tpl = templateify(data) end return tpl.render(context) if ctx == {} begin context.push(ctx) tpl.render(context) ensure context.pop end end |
- (Object) render_file(name, context = {})
Given a file name and an optional context, attempts to load and render the file as a template.
161 162 163 |
# File 'lib/mustache.rb', line 161 def render_file(name, context = {}) self.class.render_file(name, context) end |
- (Object) template
The template can be set at the instance level.
150 151 152 153 154 155 156 157 158 159 |
# File 'lib/mustache/settings.rb', line 150 def template return @template if @template # If they sent any instance-level options use that instead of the class's. if @template_path || @template_extension || @template_name || @template_file @template = templateify(File.read(template_file)) else @template = self.class.template end end |
- (Object) template=(template)
161 162 163 |
# File 'lib/mustache/settings.rb', line 161 def template=(template) @template = templateify(template) end |
- (Object) template_extension
61 62 63 |
# File 'lib/mustache/settings.rb', line 61 def template_extension @template_extension ||= self.class.template_extension end |
- (Object) template_extension=(template_extension)
65 66 67 68 |
# File 'lib/mustache/settings.rb', line 65 def template_extension=(template_extension) @template_extension = template_extension @template = nil end |
- (Object) template_file
The template file is the absolute path of the file Mustache will use as its template. By default it's ./class_name.mustache
122 123 124 |
# File 'lib/mustache/settings.rb', line 122 def template_file @template_file || "#{path}/#{template_name}.#{template_extension}" end |
- (Object) template_file=(template_file)
126 127 128 129 |
# File 'lib/mustache/settings.rb', line 126 def template_file=(template_file) @template_file = template_file @template = nil end |
- (Object) template_name
94 95 96 |
# File 'lib/mustache/settings.rb', line 94 def template_name @template_name ||= self.class.template_name end |
- (Object) template_name=(template_name)
98 99 100 101 |
# File 'lib/mustache/settings.rb', line 98 def template_name=(template_name) @template_name = template_name @template = nil end |
- (Object) template_path Also known as: path
24 25 26 |
# File 'lib/mustache/settings.rb', line 24 def template_path @template_path ||= self.class.template_path end |
- (Object) template_path=(path) Also known as: path=
28 29 30 31 |
# File 'lib/mustache/settings.rb', line 28 def template_path=(path) @template_path = File.(path) @template = nil end |
- (Object) templateify(obj)
290 291 292 |
# File 'lib/mustache.rb', line 290 def templateify(obj) self.class.templateify(obj) end |