Class: Mustache

Inherits:
Object
  • Object
show all
Defined in:
lib/mustache.rb,
lib/mustache/utils.rb,
lib/mustache/parser.rb,
lib/mustache/version.rb,
lib/mustache/context.rb,
lib/mustache/settings.rb,
lib/mustache/template.rb,
lib/mustache/generator.rb,
lib/mustache/enumerable.rb,
lib/mustache/context_miss.rb

Overview

Settings which can be configured for all view classes, a single view class, or a single Mustache instance.

Defined Under Namespace

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

Constant Summary

VERSION =
'1.0.1'
Enumerable =
Module.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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.



168
169
170
# File 'lib/mustache.rb', line 168

def self.partial(name)
  self.new.partial(name)
end

.pathObject

Alias for `template_path`



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

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



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

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)


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

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.



81
82
83
# File 'lib/mustache.rb', line 81

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.



153
154
155
# File 'lib/mustache.rb', line 153

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.


140
141
142
# File 'lib/mustache/settings.rb', line 140

def self.template
  @template ||= templateify(File.read(template_file))
end

.template=(template) ⇒ Object



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

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

.template_extensionObject

A Mustache template's default extension is 'mustache', but this can be changed.



51
52
53
# File 'lib/mustache/settings.rb', line 51

def self.template_extension
  @template_extension ||= inheritable_config_for :template_extension, 'mustache'
end

.template_extension=(template_extension) ⇒ Object



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

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



110
111
112
# File 'lib/mustache/settings.rb', line 110

def self.template_file
  @template_file || "#{path}/#{template_name}.#{template_extension}"
end

.template_file=(template_file) ⇒ Object



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

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


84
85
86
# File 'lib/mustache/settings.rb', line 84

def self.template_name
  @template_name || underscore
end

.template_name=(template_name) ⇒ Object



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

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

.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`.



202
203
204
# File 'lib/mustache/settings.rb', line 202

def self.view_namespace
  @view_namespace ||= inheritable_config_for(:view_namespace, Object)
end

.view_namespace=(namespace) ⇒ Object



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

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 “.”



218
219
220
# File 'lib/mustache/settings.rb', line 218

def self.view_path
  @view_path ||= inheritable_config_for(:view_path, '.')
end

.view_path=(path) ⇒ Object



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

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

Instance Method Details

#[](key) ⇒ Object

Context accessors.

Example:

view = Mustache.new
view[:name] = "Jon"
view.template = "Hi, {{name}}!"
view.render # => "Hi, Jon!"


136
137
138
# File 'lib/mustache.rb', line 136

def [](key)
  context[key.to_sym]
end

#[]=(key, value) ⇒ Object



140
141
142
# File 'lib/mustache.rb', line 140

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

#compiled?Boolean

Has this instance or its class already compiled a template?

Returns:

  • (Boolean)


204
205
206
# File 'lib/mustache.rb', line 204

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.



147
148
149
# File 'lib/mustache.rb', line 147

def context
  @context ||= Context.new(self)
end

#escapeHTML(str) ⇒ String

Override this to provide custom escaping.

Example:

class PersonView < Mustache
  def escapeHTML(str)
    my_html_escape_method(str)
  end
end

Parameters:

  • str (String)

    String to escape.

Returns:

  • (String)

    Escaped HTML.



199
200
201
# File 'lib/mustache.rb', line 199

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.



175
176
177
178
179
180
181
182
183
184
# File 'lib/mustache.rb', line 175

def partial(name)
  path = "#{template_path}/#{name}.#{template_extension}"

  begin
    File.read(path)
  rescue
    raise if raise_on_context_miss?
    ""
  end
end

#raise_on_context_miss=(boolean) ⇒ Object



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

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)


185
186
187
# File 'lib/mustache/settings.rb', line 185

def raise_on_context_miss?
  self.class.raise_on_context_miss? || @raise_on_context_miss
end

#render(data = template, ctx = {}) ⇒ String

Parses our fancy pants template file and returns normal file with all special {tags} and Mustache.{{#sections}replaced{/sections}.

Examples

@view.render("Hi {{thing}}!", :thing => :world)

View.template = "Hi {{thing}}!"
@view = View.new
@view.render(:thing => :world)

Parameters:

  • data (String, Hash) (defaults to: template)

    A String template or a Hash context. If a Hash is given, we'll try to figure out the template from the class.

  • ctx (Hash) (defaults to: {})

    A Hash context if `data` is a String template.

Returns:

  • (String)

    Returns a rendered version of a template.



102
103
104
105
106
107
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 102

def render(data = template, ctx = {})
  case data
  when Hash
    ctx = data
  when Symbol
    self.template_name = data
  end

  tpl = case data
  when Hash
    templateify(template)
  when Symbol
    templateify(template)
  else
    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.



159
160
161
# File 'lib/mustache.rb', line 159

def render_file(name, context = {})
  self.class.render_file(name, context)
end

#templateObject

The template can be set at the instance level.



149
150
151
152
153
154
155
156
157
158
# File 'lib/mustache/settings.rb', line 149

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



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

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

#template_extensionObject



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

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

#template_extension=(template_extension) ⇒ Object



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

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



121
122
123
# File 'lib/mustache/settings.rb', line 121

def template_file
  @template_file || "#{path}/#{template_name}.#{template_extension}"
end

#template_file=(template_file) ⇒ Object



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

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

#template_nameObject



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

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

#template_name=(template_name) ⇒ Object



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

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



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

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