1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
require 'mustache/enumerable'
require 'mustache/template'
require 'mustache/context'
require 'mustache/settings'
require 'mustache/utils'
# Mustache is the base class from which your Mustache subclasses
# should inherit (though it can be used on its own).
#
# The typical Mustache workflow is as follows:
#
# * Create a Mustache subclass: class Stats < Mustache
# * Create a template: stats.mustache
# * Instantiate an instance: view = Stats.new
# * Render that instance: view.render
#
# You can skip the instantiation by calling `Stats.render` directly.
#
# While Mustache will do its best to load and render a template for
# you, this process is completely customizable using a few options.
#
# All settings can be overriden at the class level.
#
# For example, going with the above example, we can use
# `Stats.template_path = "/usr/local/templates"` to specify the path
# Mustache uses to find templates.
#
# Here are the available options:
#
# * template_path
#
# The `template_path` setting determines the path Mustache uses when
# looking for a template. By default it is "."
# Setting it to /usr/local/templates, for example, means (given all
# other settings are default) a Mustache subclass `Stats` will try to
# load /usr/local/templates/stats.mustache
#
# * template_extension
#
# The `template_extension` is the extension Mustache uses when looking
# for template files. By default it is "mustache"
#
# * template_file
#
# You can tell Mustache exactly which template to use with this
# setting. It can be a relative or absolute path.
#
# * template
#
# Sometimes you want Mustache to render a string, not a file. In those
# cases you may set the `template` setting. For example:
#
# >> Mustache.render("Hello {{planet}}", :planet => "World!")
# => "Hello World!"
#
# The `template` setting is also available on instances.
#
# view = Mustache.new
# view.template = "Hi, {{person}}!"
# view[:person] = 'Mom'
# view.render # => Hi, mom!
#
# * view_namespace
#
# To make life easy on those developing Mustache plugins for web frameworks or
# other libraries, Mustache will attempt to load view classes (i.e. Mustache
# subclasses) using the `view_class` class method. The `view_namespace` tells
# Mustache under which constant view classes live. By default it is `Object`.
#
# * view_path
#
# Similar to `template_path`, the `view_path` option tells Mustache where to look
# for files containing view classes when using the `view_class` method.
#
class Mustache
# Initialize a new Mustache instance.
#
# @param [Hash] options An options hash
# @option options [String] template_path
# @option options [String] template_extension
# @option options [String] template_file
# @option options [String] template
# @option options [String] view_namespace
# @option options [String] view_path
def initialize(options = {})
@options = options
initialize_settings
end
# Instantiates an instance of this class and calls `render` with
# the passed args.
#
# @return A rendered String version of a template.
def self.render(*args)
new.render(*args)
end
# Parses our fancy pants template file and returns normal file with
# all special {{tags}} and {{#sections}}replaced{{/sections}}.
#
# @example Render view
# @view.render("Hi {{thing}}!", :thing => :world)
#
# @example Set view template and then render
# View.template = "Hi {{thing}}!"
# @view = View.new
# @view.render(:thing => :world)
#
# @param [String,Hash] data A String template or a Hash context.
# If a Hash is given, we'll try to figure
# out the template from the class.
# @param [Hash] ctx A Hash context if `data` is a String template.
# @return [String] Returns a rendered version of a template.
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
# Context accessors.
#
# @example Context accessors
# view = Mustache.new
# view[:name] = "Jon"
# view.template = "Hi, {{name}}!"
# view.render # => "Hi, Jon!"
def [](key)
context[key.to_sym]
end
def []=(key, value)
context[key.to_sym] = value
end
# 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.
def context
@context ||= Context.new(self)
end
# Given a file name and an optional context, attempts to load and
# render the file as a template.
def self.render_file(name, context = {})
render(partial(name), context)
end
# Given a file name and an optional context, attempts to load and
# render the file as a template.
def render_file(name, context = {})
self.class.render_file(name, context)
end
# 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.
def self.partial(name)
self.new.partial(name)
end
# 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.
def partial(name)
path = "#{template_path}/#{name}.#{template_extension}"
begin
File.read(path)
rescue
raise if raise_on_context_miss?
""
end
end
# Override this to provide custom escaping.
# By default it uses `CGI.escapeHTML`.
#
# @example Overriding #escape
# class PersonView < Mustache
# def escape(value)
# my_html_escape_method(value.to_s)
# end
# end
#
# @param [Object] value Value to escape.
# @return [String] Escaped content.
def escape(value)
self.escapeHTML(value.to_s)
end
# Override this to provide custom escaping.
#
# @example Overriding #escapeHTML
# class PersonView < Mustache
# def escapeHTML(str)
# my_html_escape_method(str)
# end
# end
#
# @deprecated Use {#escape} instead.
#
# Note that {#escape} can receive any kind of object.
# If your override logic is expecting a string, you will
# have to call to_s on it yourself.
# @param [String] str String to escape.
# @return [String] Escaped HTML.
def escapeHTML(str)
CGI.escapeHTML(str)
end
# Has this instance or its class already compiled a template?
def compiled?
(@template && @template.is_a?(Template)) || self.class.compiled?
end
private
# When given a symbol or string representing a class, will try to produce an
# appropriate view class.
#
# @example
# Mustache.view_namespace = Hurl::Views
# Mustache.view_class(:Partial) # => Hurl::Views::Partial
def self.view_class(name)
name = classify(name.to_s)
# Emptiness begets emptiness.
return Mustache if name.to_s.empty?
name = "#{view_namespace}::#{name}"
const = rescued_const_get(name)
return const if const
const_from_file(name)
end
def self.rescued_const_get name
const_get(name, true) || Mustache
rescue NameError
nil
end
def self.const_from_file name
file_name = underscore(name)
file_path = "#{view_path}/#{file_name}.rb"
return Mustache unless File.exist?(file_path)
require file_path.chomp('.rb')
rescued_const_get(name)
end
# Has this template already been compiled? Compilation is somewhat
# expensive so it may be useful to check this before attempting it.
def self.compiled?
@template.is_a? Template
end
# template_partial => TemplatePartial
# template/partial => Template::Partial
def self.classify(underscored)
Mustache::Utils::String.new(underscored).classify
end
# TemplatePartial => template_partial
# Template::Partial => template/partial
# Takes a string but defaults to using the current class' name.
def self.underscore(classified = name)
classified = superclass.name if classified.to_s.empty?
Mustache::Utils::String.new(classified).underscore(view_namespace)
end
# @param [Template,String] obj Turns `obj` into a template
# @param [Hash] options Options for template creation
def self.templateify(obj, options = {})
obj.is_a?(Template) ? obj : Template.new(obj, options)
end
def templateify(obj)
opts = {:partial_resolver => self.method(:partial)}
opts.merge!(@options) if @options.is_a?(Hash)
self.class.templateify(obj, opts)
end
# Return the value of the configuration setting on the superclass, or return
# the default.
#
# @param [Symbol] attr_name Name of the attribute. It should match
# the instance variable.
# @param [Object] default Default value to use if the superclass does
# not respond.
#
# @return Inherited or default configuration setting.
def self.inheritable_config_for(attr_name, default)
superclass.respond_to?(attr_name) ? superclass.send(attr_name) : default
end
end
|