Class: ERBook::Template
- Inherits:
-
Ember::Template
- Object
- Ember::Template
- ERBook::Template
- Defined in:
- lib/erbook/template.rb
Overview
eRuby template that provides access to the underlying result buffer (which contains the result of template evaluation thus far) and provides sandboxing for isolated template rendering.
Defined Under Namespace
Classes: Sandbox
Instance Attribute Summary collapse
-
#sandbox ⇒ Object
readonly
Returns the value of attribute sandbox.
Instance Method Summary collapse
-
#buffer ⇒ Object
Returns the result of template evaluation thus far.
-
#initialize(source, input, unindent = true, safe_level = nil) ⇒ Template
constructor
Parameters.
-
#render ⇒ Object
Returns the output of evaluating this template inside the given context.
-
#render_with(inst_vars = {}) ⇒ Object
Renders this template within a fresh sandbox that is populated with the given instance variables, whose names must be prefixed with ‘@’.
Constructor Details
#initialize(source, input, unindent = true, safe_level = nil) ⇒ Template
Parameters
- source
-
Replacement for the ambiguous ‘(erb)’ identifier in stack traces; so that the user can better determine the source of an error.
- input
-
A string containing eRuby directives.
- unindent
-
If true, then all content blocks will be unindented hierarchically,
- safe_level
-
See safe_level in ERB::new().
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/erbook/template.rb', line 33 def initialize source, input, unindent = true, safe_level = nil super input, :result_variable => :@buffer, :source_file => source, :unindent => unindent, :shorthand => true, :infer_end => true @sandbox = Sandbox.new if $DEBUG IO.popen('cat -n', 'w+') do |io| io.write self.program io.close_write STDERR.puts io.read end end end |
Instance Attribute Details
#sandbox ⇒ Object (readonly)
Returns the value of attribute sandbox.
15 16 17 |
# File 'lib/erbook/template.rb', line 15 def sandbox @sandbox end |
Instance Method Details
#buffer ⇒ Object
Returns the result of template evaluation thus far.
64 65 66 |
# File 'lib/erbook/template.rb', line 64 def buffer @sandbox.instance_variable_get(:@buffer) end |
#render ⇒ Object
Returns the output of evaluating this template inside the given context.
If no context is given, then the sandbox of this template is used.
57 58 59 |
# File 'lib/erbook/template.rb', line 57 def render super @sandbox.instance_eval('binding') end |
#render_with(inst_vars = {}) ⇒ Object
Renders this template within a fresh sandbox that is populated with the given instance variables, whose names must be prefixed with ‘@’.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/erbook/template.rb', line 72 def render_with inst_vars = {} old_sandbox = @sandbox begin @sandbox = Sandbox.new inst_vars.each_pair do |var, val| @sandbox.instance_variable_set var, val end render ensure @sandbox = old_sandbox end end |