Class: Malt::Engines::Erb
Overview
ERB template implementation.
http://www.ruby-doc.org/stdlib/libdoc/erb/rdoc/classes/ERB.html
ERB templates accept two options. safe
sets the safe mode for rendering the template and trim
is a weird string that controls a few rendering options –it can be ‘%’ and/or ‘>’ or ‘<>’.
Instance Attribute Summary
Attributes inherited from Abstract
Instance Method Summary collapse
-
#compile(params = {}) ⇒ Object
Compile ERB template into Ruby source code.
-
#intermediate(params = {}) ⇒ Object
Returns instance of underlying ::ERB class.
-
#render(params = {}, &yld) ⇒ Object
Render ERB template.
Methods inherited from Abstract
#cache?, default, #initialize, register
Constructor Details
This class inherits a constructor from Malt::Engines::Abstract
Instance Method Details
#compile(params = {}) ⇒ Object
Compile ERB template into Ruby source code.
41 42 43 44 45 46 47 48 |
# File 'lib/malt/engines/erb.rb', line 41 def compile(params={}) file = params[:file] if cache? @source[file] ||= intermediate(params).src else intermediate(params).src end end |
#intermediate(params = {}) ⇒ Object
Returns instance of underlying ::ERB class.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/malt/engines/erb.rb', line 51 def intermediate(params={}) text = params[:text] file = params[:file] opts = (params) safe = opts[:safe] trim = opts[:trim] if cache? @cache[file] ||= ::ERB.new(text, safe, trim) else ::ERB.new(text, safe, trim) end end |
#render(params = {}, &yld) ⇒ Object
Render ERB template.
The params
can be:
-
:text - text of erb document
-
:file - file name where text was read (or nil)
-
:data - data source for template interpolation
-
:safe -
-
:trim -
Returns a String.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/malt/engines/erb.rb', line 27 def render(params={}, &yld) text = params[:text] file = params[:file] data = params[:data] data = make_binding(data, &yld) if settings[:precompile] == false intermediate(params).result(data) else ruby = compile(params) eval(ruby, data, file) end end |