Class: Papercraft::Template
- Inherits:
-
Object
- Object
- Papercraft::Template
- Defined in:
- lib/papercraft/template.rb
Overview
Template wrapper class. This class can be used to distinguish between Papercraft templates and other kinds of procs.
Instance Attribute Summary collapse
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
-
#proc ⇒ Object
readonly
Returns the value of attribute proc.
Instance Method Summary collapse
-
#__papercraft_compiled_proc ⇒ Proc
Returns the compiled proc for the template.
-
#apply ⇒ Papercraft::Template
Applies the given parameters and block to the template, returning an applied template.
-
#initialize(proc = nil, mode: :html, &block) ⇒ Template
constructor
A new instance of Template.
-
#render ⇒ String
(also: #call)
Renders the template.
Constructor Details
#initialize(proc = nil, mode: :html, &block) ⇒ Template
Returns a new instance of Template.
11 12 13 14 15 16 |
# File 'lib/papercraft/template.rb', line 11 def initialize(proc = nil, mode: :html, &block) @proc = proc || block raise ArgumentError, "No template proc given" if !@proc @mode = mode end |
Instance Attribute Details
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
7 8 9 |
# File 'lib/papercraft/template.rb', line 7 def mode @mode end |
#proc ⇒ Object (readonly)
Returns the value of attribute proc.
7 8 9 |
# File 'lib/papercraft/template.rb', line 7 def proc @proc end |
Instance Method Details
#__papercraft_compiled_proc ⇒ Proc
Returns the compiled proc for the template.
37 38 39 |
# File 'lib/papercraft/template.rb', line 37 def __papercraft_compiled_proc @proc.__papercraft_compiled_proc(mode: @mode) end |
#apply ⇒ Papercraft::Template
Applies the given parameters and block to the template, returning an applied template.
30 31 32 |
# File 'lib/papercraft/template.rb', line 30 def apply(*, **, &) Template.new(Papercraft.apply(@proc, *, **, &), mode: @mode) end |
#render ⇒ String Also known as: call
Renders the template.
21 22 23 |
# File 'lib/papercraft/template.rb', line 21 def render(*, **, &) (mode == :xml) ? Papercraft.xml(@proc, *, **, &) : Papercraft.html(@proc, *, **, &) end |