Class: Mustache

Inherits:
Object
  • Object
show all
Defined in:
lib/mustache.rb,
lib/mustache/parser.rb,
lib/mustache/sinatra.rb,
lib/mustache/version.rb,
lib/mustache/context.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

Rack::Bug::MustachePanel::View

Defined Under Namespace

Modules: Sinatra Classes: Context, ContextMiss, Generator, Parser, Template

Constant Summary

Version =
VERSION = '0.99.5'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.classify(underscored) ⇒ Object

template_partial => TemplatePartial template/partial => Template::Partial



253
254
255
256
257
258
259
# File 'lib/mustache.rb', line 253

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

.compiled?Boolean

Has this template already been compiled? Compilation is somewhat expensive so it may be useful to check this before attempting it.

Returns:

  • (Boolean)


242
243
244
# File 'lib/mustache.rb', line 242

def self.compiled?
  @template.is_a? Template
end

.const_get!(name) ⇒ Object

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



232
233
234
235
236
237
238
# File 'lib/mustache.rb', line 232

def self.const_get!(name)
  name.split('::').inject(Object) do |klass, cname|
    klass.const_get(cname)
  end
rescue NameError
  nil
end

.inheritable_config_for(attr_name, default) ⇒ Object

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.



293
294
295
# File 'lib/mustache.rb', line 293

def self.inheritable_config_for(attr_name, default)
  superclass.respond_to?(attr_name) ? superclass.send(attr_name) : default
end

.partial(name) ⇒ Object

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

.pathObject

Alias for `template_path`



34
35
36
# File 'lib/mustache/settings.rb', line 34

def self.path
  template_path
end

.path=(path) ⇒ Object

Alias for `template_path`



40
41
42
# File 'lib/mustache/settings.rb', line 40

def self.path=(path)
  self.template_path = path
end

.raise_on_context_miss=(boolean) ⇒ Object



181
182
183
# File 'lib/mustache/settings.rb', line 181

def self.raise_on_context_miss=(boolean)
  @raise_on_context_miss = boolean
end

.raise_on_context_miss?Boolean

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.

Returns:

  • (Boolean)


177
178
179
# File 'lib/mustache/settings.rb', line 177

def self.raise_on_context_miss?
  @raise_on_context_miss
end

.render(*args) ⇒ Object

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

.render_file(name, context = {}) ⇒ Object

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

.templateObject

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

.template=(template) ⇒ Object



145
146
147
# File 'lib/mustache/settings.rb', line 145

def self.template=(template)
  @template = templateify(template)
end

.template_extensionObject

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

.template_extension=(template_extension) ⇒ Object



56
57
58
59
# File 'lib/mustache/settings.rb', line 56

def self.template_extension=(template_extension)
  @template_extension = template_extension
  @template = nil
end

.template_fileObject

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

.template_file=(template_file) ⇒ Object



115
116
117
118
# File 'lib/mustache/settings.rb', line 115

def self.template_file=(template_file)
  @template_file = template_file
  @template = nil
end

.template_nameObject

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

.template_name=(template_name) ⇒ Object



89
90
91
92
# File 'lib/mustache/settings.rb', line 89

def self.template_name=(template_name)
  @template_name = template_name
  @template = nil
end

.template_pathObject

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

.template_path=(path) ⇒ Object



19
20
21
22
# File 'lib/mustache/settings.rb', line 19

def self.template_path=(path)
  @template_path = File.expand_path(path)
  @template = nil
end

.templateify(obj) ⇒ Object

Turns a string into a Mustache::Template. If passed a Template, returns it.



278
279
280
# File 'lib/mustache.rb', line 278

def self.templateify(obj)
  obj.is_a?(Template) ? obj : Template.new(obj.to_s)
end

.to_htmlObject

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

.to_textObject

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

.underscore(classified = name) ⇒ Object

TemplatePartial => template_partial Template::Partial => template/partial Takes a string but defaults to using the current class' name.



264
265
266
267
268
269
270
271
272
273
274
# File 'lib/mustache.rb', line 264

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

.view_class(name) ⇒ Object

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
# File 'lib/mustache.rb', line 204

def self.view_class(name)
  name = classify(name.to_s)

  # Emptiness begets emptiness.
  return Mustache if name.to_s == ''

  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

.view_namespaceObject

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

.view_namespace=(namespace) ⇒ Object



207
208
209
# File 'lib/mustache/settings.rb', line 207

def self.view_namespace=(namespace)
  @view_namespace = namespace
end

.view_pathObject

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

.view_path=(path) ⇒ Object



223
224
225
# File 'lib/mustache/settings.rb', line 223

def self.view_path=(path)
  @view_path = path
end

Instance Method Details

#[](key) ⇒ Object

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

#[]=(key, value) ⇒ Object



142
143
144
# File 'lib/mustache.rb', line 142

def []=(key, value)
  context[key.to_sym] = value
end

#compiled?Boolean

Has this instance or its class already compiled a template?

Returns:

  • (Boolean)


247
248
249
# File 'lib/mustache.rb', line 247

def compiled?
  (@template && @template.is_a?(Template)) || self.class.compiled?
end

#contextObject

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

#escapeHTML(str) ⇒ Object

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

#partial(name) ⇒ Object

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

#raise_on_context_miss=(boolean) ⇒ Object



190
191
192
# File 'lib/mustache/settings.rb', line 190

def raise_on_context_miss=(boolean)
  @raise_on_context_miss = boolean
end

#raise_on_context_miss?Boolean

Instance level version of `Mustache.raise_on_context_miss?`

Returns:

  • (Boolean)


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

#render(data = template, ctx = {}) ⇒ Object 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

#render_file(name, context = {}) ⇒ Object

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

#templateObject

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

#template=(template) ⇒ Object



161
162
163
# File 'lib/mustache/settings.rb', line 161

def template=(template)
  @template = templateify(template)
end

#template_extensionObject



61
62
63
# File 'lib/mustache/settings.rb', line 61

def template_extension
  @template_extension ||= self.class.template_extension
end

#template_extension=(template_extension) ⇒ Object



65
66
67
68
# File 'lib/mustache/settings.rb', line 65

def template_extension=(template_extension)
  @template_extension = template_extension
  @template = nil
end

#template_fileObject

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

#template_file=(template_file) ⇒ Object



126
127
128
129
# File 'lib/mustache/settings.rb', line 126

def template_file=(template_file)
  @template_file = template_file
  @template = nil
end

#template_nameObject



94
95
96
# File 'lib/mustache/settings.rb', line 94

def template_name
  @template_name ||= self.class.template_name
end

#template_name=(template_name) ⇒ Object



98
99
100
101
# File 'lib/mustache/settings.rb', line 98

def template_name=(template_name)
  @template_name = template_name
  @template = nil
end

#template_pathObject Also known as: path



24
25
26
# File 'lib/mustache/settings.rb', line 24

def template_path
  @template_path ||= self.class.template_path
end

#template_path=(path) ⇒ Object Also known as: path=



28
29
30
31
# File 'lib/mustache/settings.rb', line 28

def template_path=(path)
  @template_path = File.expand_path(path)
  @template = nil
end

#templateify(obj) ⇒ Object



282
283
284
# File 'lib/mustache.rb', line 282

def templateify(obj)
  self.class.templateify(obj)
end