Class: ORB::Template
- Inherits:
-
Object
- Object
- ORB::Template
- Defined in:
- lib/orb/template.rb
Instance Attribute Summary collapse
-
#doc ⇒ Object
readonly
Returns the value of attribute doc.
Class Method Summary collapse
-
.parse(source, opts = {}) ⇒ Object
create a new ‘Template` instance and parse the given source.
Instance Method Summary collapse
-
#assigns ⇒ Object
Local assigns.
-
#errors ⇒ Object
Parsing and rendering errors.
-
#initialize(**opts) ⇒ Template
constructor
Create a new ‘Template` instance.
-
#parse(source) ⇒ Object
Parses the given ‘source` and returns `self` for chaining.
-
#render(*args) ⇒ Object
Render the template with the given ‘assigns`, which is a hash of local variables.
Constructor Details
#initialize(**opts) ⇒ Template
Create a new ‘Template` instance. Use `Template.parse` instead.
17 18 19 |
# File 'lib/orb/template.rb', line 17 def initialize(**opts) = opts end |
Instance Attribute Details
#doc ⇒ Object (readonly)
Returns the value of attribute doc.
5 6 7 |
# File 'lib/orb/template.rb', line 5 def doc @doc end |
Class Method Details
.parse(source, opts = {}) ⇒ Object
create a new ‘Template` instance and parse the given source
9 10 11 12 13 |
# File 'lib/orb/template.rb', line 9 def parse(source, opts = {}) template = Template.new(**opts) template.parse(source) template end |
Instance Method Details
#assigns ⇒ Object
Local assigns
28 29 30 |
# File 'lib/orb/template.rb', line 28 def assigns @assigns ||= {} end |
#errors ⇒ Object
Parsing and rendering errors
33 34 35 |
# File 'lib/orb/template.rb', line 33 def errors @errors ||= [] end |
#parse(source) ⇒ Object
Parses the given ‘source` and returns `self` for chaining.
22 23 24 25 |
# File 'lib/orb/template.rb', line 22 def parse(source) @doc = Document.new(tokenize(source)) self end |
#render(*args) ⇒ Object
Render the template with the given ‘assigns`, which is a hash of local variables.
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 |
# File 'lib/orb/template.rb', line 38 def render(*args) # if we don't have a Document node, render to an empty string retun "" unless @doc # Determine the rendering context, which can either be a hash of local variables # or an instance of `ORB::RenderContext`. render_context = case args.first when ORB::RenderContext args.shift when Hash assigns.merge!(args.shift) ORB::RenderContext.new(assigns) when nil ORB::RenderContext.new(assigns) else raise ArgumentError, "Expected a hash of local assigns or a ORB::RenderContext." end # Render loop begin @doc.render(render_context) ensure @errors = render_context.errors end end |