Class: Braintrust::Vendor::Mustache
- Inherits:
-
Object
- Object
- Braintrust::Vendor::Mustache
- Defined in:
- lib/braintrust/vendor/mustache/mustache.rb,
lib/braintrust/vendor/mustache/utils.rb,
lib/braintrust/vendor/mustache/parser.rb,
lib/braintrust/vendor/mustache/context.rb,
lib/braintrust/vendor/mustache/settings.rb,
lib/braintrust/vendor/mustache/template.rb,
lib/braintrust/vendor/mustache/generator.rb,
lib/braintrust/vendor/mustache/enumerable.rb,
lib/braintrust/vendor/mustache/context_miss.rb
Overview
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.
Defined Under Namespace
Modules: Utils Classes: Context, ContextMiss, Generator, Parser, Template
Constant Summary collapse
- Enumerable =
Module.new
Class Method Summary collapse
-
.classify(underscored) ⇒ Object
template_partial => TemplatePartial template/partial => Template::Partial.
-
.compiled? ⇒ Boolean
Has this template already been compiled? Compilation is somewhat expensive so it may be useful to check this before attempting it.
- .const_from_file(name) ⇒ Object
-
.inheritable_config_for(attr_name, default) ⇒ Object
Return the value of the configuration setting on the superclass, or return the default.
- .inherited(subclass) ⇒ Object
- .initialize_settings ⇒ Object
-
.partial(name) ⇒ Object
Given a name, attempts to read a file and return the contents as a string.
-
.path ⇒ Object
Alias for ‘template_path`.
-
.path=(path) ⇒ Object
Alias for ‘template_path`.
- .raise_on_context_miss=(boolean) ⇒ Object
-
.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.
-
.render(*args) ⇒ Object
Instantiates an instance of this class and calls ‘render` with the passed args.
-
.render_file(name, context = {}) ⇒ Object
Given a file name and an optional context, attempts to load and render the file as a template.
- .rescued_const_get(name) ⇒ Object
-
.template ⇒ Object
The template is the actual string Mustache uses as its template.
- .template=(template) ⇒ Object
-
.template_extension ⇒ Object
A Mustache template’s default extension is ‘mustache’, but this can be changed.
- .template_extension=(template_extension) ⇒ Object
-
.template_file ⇒ Object
The template file is the absolute path of the file Mustache will use as its template.
- .template_file=(template_file) ⇒ Object
-
.template_name ⇒ Object
The template name is the Mustache template file without any extension or other information.
- .template_name=(template_name) ⇒ Object
-
.template_path ⇒ Object
The template path informs your Mustache view where to look for its corresponding template.
- .template_path=(path) ⇒ Object
- .templateify(obj, options = {}) ⇒ Object
-
.underscore(classified = name) ⇒ Object
TemplatePartial => template_partial Template::Partial => template/partial Takes a string but defaults to using the current class’ name.
-
.view_class(name) ⇒ Object
When given a symbol or string representing a class, will try to produce an appropriate view class.
-
.view_namespace ⇒ Object
The constant under which Mustache will look for views when autoloading.
- .view_namespace=(namespace) ⇒ Object
-
.view_path ⇒ Object
Mustache searches the view path for .rb files to require when asked to find a view class.
- .view_path=(path) ⇒ Object
Instance Method Summary collapse
-
#[](key) ⇒ Object
Context accessors.
- #[]=(key, value) ⇒ Object
-
#compiled? ⇒ Boolean
Has this instance or its class already compiled a template?.
-
#context ⇒ Object
A helper method which gives access to the context at a given time.
-
#escape(value) ⇒ String
BRAINTRUST MODIFICATION: No HTML escaping for LLM prompts.
-
#escapeHTML(str) ⇒ Object
deprecated
Deprecated.
Use #escape instead.
-
#initialize(options = {}) ⇒ Mustache
constructor
Initialize a new Mustache instance.
- #initialize_settings ⇒ Object
-
#partial(name) ⇒ Object
Override this in your subclass if you want to do fun things like reading templates from a database.
- #raise_on_context_miss=(boolean) ⇒ Object
-
#raise_on_context_miss? ⇒ Boolean
Instance level version of ‘Mustache.raise_on_context_miss?`.
-
#render(data = template, ctx = {}) ⇒ String
Parses our fancy pants template file and returns normal file with all special {tags} and {{#sections}replaced{/sections}.
-
#render_file(name, context = {}) ⇒ Object
Given a file name and an optional context, attempts to load and render the file as a template.
-
#template ⇒ Object
The template can be set at the instance level.
- #template=(template) ⇒ Object
- #template_extension ⇒ Object
- #template_extension=(template_extension) ⇒ Object
-
#template_file ⇒ Object
The template file is the absolute path of the file Mustache will use as its template.
- #template_file=(template_file) ⇒ Object
- #template_name ⇒ Object
- #template_name=(template_name) ⇒ Object
- #template_path ⇒ Object (also: #path)
- #template_path=(path) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Mustache
Initialize a new Mustache instance.
39 40 41 42 43 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 39 def initialize( = {}) @options = initialize_settings end |
Class Method Details
.classify(underscored) ⇒ Object
template_partial => TemplatePartial template/partial => Template::Partial
221 222 223 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 221 def self.classify(underscored) Mustache::Utils::String.new(underscored).classify end |
.compiled? ⇒ Boolean
Has this template already been compiled? Compilation is somewhat expensive so it may be useful to check this before attempting it.
215 216 217 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 215 def self.compiled? @template.is_a? Template end |
.const_from_file(name) ⇒ Object
203 204 205 206 207 208 209 210 211 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 203 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 |
.inheritable_config_for(attr_name, default) ⇒ Object
Return the value of the configuration setting on the superclass, or return the default.
255 256 257 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 255 def self.inheritable_config_for(attr_name, default) superclass.respond_to?(attr_name) ? superclass.send(attr_name) : default end |
.inherited(subclass) ⇒ Object
33 34 35 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 33 def self.inherited(subclass) subclass.initialize_settings end |
.initialize_settings ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 22 def self.initialize_settings @template = nil @template_path = nil @template_extension = nil @template_name = nil @template_file = nil @raise_on_context_miss = nil 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.
135 136 137 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 135 def self.partial(name) new.partial(name) end |
.path ⇒ Object
Alias for ‘template_path`
67 68 69 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 67 def self.path template_path end |
.path=(path) ⇒ Object
Alias for ‘template_path`
72 73 74 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 72 def self.path=(path) self.template_path = path end |
.raise_on_context_miss=(boolean) ⇒ Object
207 208 209 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 207 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.
203 204 205 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 203 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.
49 50 51 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 49 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.
120 121 122 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 120 def self.render_file(name, context = {}) render(partial(name), context) end |
.rescued_const_get(name) ⇒ Object
197 198 199 200 201 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 197 def self.rescued_const_get(name) const_get(name, true) || Mustache rescue NameError nil end |
.template ⇒ Object
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.
168 169 170 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 168 def self.template @template ||= templateify(File.read(template_file)) end |
.template=(template) ⇒ Object
172 173 174 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 172 def self.template=(template) @template = templateify(template) end |
.template_extension ⇒ Object
A Mustache template’s default extension is ‘mustache’, but this can be changed.
82 83 84 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 82 def self.template_extension @template_extension ||= inheritable_config_for :template_extension, "mustache" end |
.template_extension=(template_extension) ⇒ Object
86 87 88 89 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 86 def self.template_extension=(template_extension) @template_extension = template_extension @template = nil end |
.template_file ⇒ Object
The template file is the absolute path of the file Mustache will use as its template. By default it’s ./class_name.mustache
139 140 141 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 139 def self.template_file @template_file || "#{path}/#{template_name}.#{template_extension}" end |
.template_file=(template_file) ⇒ Object
143 144 145 146 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 143 def self.template_file=(template_file) @template_file = template_file @template = nil end |
.template_name ⇒ Object
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
114 115 116 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 114 def self.template_name @template_name || underscore end |
.template_name=(template_name) ⇒ Object
118 119 120 121 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 118 def self.template_name=(template_name) @template_name = template_name @template = nil end |
.template_path ⇒ Object
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”
47 48 49 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 47 def self.template_path @template_path ||= inheritable_config_for :template_path, "." end |
.template_path=(path) ⇒ Object
51 52 53 54 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 51 def self.template_path=(path) @template_path = File.(path) @template = nil end |
.templateify(obj, options = {}) ⇒ Object
236 237 238 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 236 def self.templateify(obj, = {}) obj.is_a?(Template) ? obj : Template.new(obj, ) end |
.underscore(classified = name) ⇒ Object
TemplatePartial => template_partial Template::Partial => template/partial Takes a string but defaults to using the current class’ name.
228 229 230 231 232 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 228 def self.underscore(classified = name) classified = superclass.name if classified.to_s.empty? Mustache::Utils::String.new(classified).underscore(view_namespace) end |
.view_class(name) ⇒ Object
When given a symbol or string representing a class, will try to produce an appropriate view class.
183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 183 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 |
.view_namespace ⇒ Object
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`.
228 229 230 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 228 def self.view_namespace @view_namespace ||= inheritable_config_for(:view_namespace, Object) end |
.view_namespace=(namespace) ⇒ Object
232 233 234 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 232 def self.view_namespace=(namespace) @view_namespace = namespace end |
.view_path ⇒ Object
Mustache searches the view path for .rb files to require when asked to find a view class. Defaults to “.”
243 244 245 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 243 def self.view_path @view_path ||= inheritable_config_for(:view_path, ".") end |
.view_path=(path) ⇒ Object
247 248 249 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 247 def self.view_path=(path) @view_path = path end |
Instance Method Details
#[](key) ⇒ Object
Context accessors.
103 104 105 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 103 def [](key) context[key.to_sym] end |
#[]=(key, value) ⇒ Object
107 108 109 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 107 def []=(key, value) context[key.to_sym] = value end |
#compiled? ⇒ Boolean
Has this instance or its class already compiled a template?
171 172 173 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 171 def compiled? (@template && @template.is_a?(Template)) || self.class.compiled? end |
#context ⇒ Object
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.
114 115 116 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 114 def context @context ||= Context.new(self) end |
#escape(value) ⇒ String
BRAINTRUST MODIFICATION: No HTML escaping for LLM prompts. Original mustache uses CGI.escapeHTML which would turn characters like < > & “ into HTML entities. For LLM prompts, we want the raw text without escaping.
160 161 162 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 160 def escape(value) value.to_s end |
#escapeHTML(str) ⇒ Object
Use #escape instead.
Kept for compatibility but also does no escaping.
166 167 168 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 166 def escapeHTML(str) str.to_s end |
#initialize_settings ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 13 def initialize_settings @template = nil @template_path = nil @template_extension = nil @template_name = nil @template_file = nil @raise_on_context_miss = nil 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.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 142 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
216 217 218 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 216 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?`
212 213 214 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 212 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 Braintrust::Vendor::Mustache.{{#sections}replaced{/sections}.
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 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 69 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.
126 127 128 |
# File 'lib/braintrust/vendor/mustache/mustache.rb', line 126 def render_file(name, context = {}) self.class.render_file(name, context) end |
#template ⇒ Object
The template can be set at the instance level.
177 178 179 180 181 182 183 184 185 186 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 177 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
188 189 190 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 188 def template=(template) @template = templateify(template) end |
#template_extension ⇒ Object
91 92 93 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 91 def template_extension @template_extension ||= self.class.template_extension end |
#template_extension=(template_extension) ⇒ Object
95 96 97 98 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 95 def template_extension=(template_extension) @template_extension = template_extension @template = nil end |
#template_file ⇒ Object
The template file is the absolute path of the file Mustache will use as its template. By default it’s ./class_name.mustache
150 151 152 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 150 def template_file @template_file || "#{path}/#{template_name}.#{template_extension}" end |
#template_file=(template_file) ⇒ Object
154 155 156 157 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 154 def template_file=(template_file) @template_file = template_file @template = nil end |
#template_name ⇒ Object
123 124 125 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 123 def template_name @template_name ||= self.class.template_name end |
#template_name=(template_name) ⇒ Object
127 128 129 130 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 127 def template_name=(template_name) @template_name = template_name @template = nil end |
#template_path ⇒ Object Also known as: path
56 57 58 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 56 def template_path @template_path ||= self.class.template_path end |
#template_path=(path) ⇒ Object
61 62 63 64 |
# File 'lib/braintrust/vendor/mustache/settings.rb', line 61 def template_path=(path) @template_path = File.(path) @template = nil end |