Class: Papercraft::Template

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(proc = nil, mode: :html, &block) ⇒ Template

Returns a new instance of Template.

Parameters:

  • proc (Proc) (defaults to: nil)

    template proc

  • mode (Symbol) (defaults to: :html)

    mode (:html, :xml)

Raises:

  • (ArgumentError)


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

#modeObject (readonly)

Returns the value of attribute mode.



7
8
9
# File 'lib/papercraft/template.rb', line 7

def mode
  @mode
end

#procObject (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_procProc

Returns the compiled proc for the template.

Returns:

  • (Proc)

    compiled proc



37
38
39
# File 'lib/papercraft/template.rb', line 37

def __papercraft_compiled_proc
  @proc.__papercraft_compiled_proc(mode: @mode)
end

#applyPapercraft::Template

Applies the given parameters and block to the template, returning an applied template.

Returns:



30
31
32
# File 'lib/papercraft/template.rb', line 30

def apply(*, **, &)
  Template.new(Papercraft.apply(@proc, *, **, &), mode: @mode)
end

#renderString Also known as: call

Renders the template.

Returns:

  • (String)

    generated HTML



21
22
23
# File 'lib/papercraft/template.rb', line 21

def render(*, **, &)
  (mode == :xml) ? Papercraft.xml(@proc, *, **, &) : Papercraft.html(@proc, *, **, &)
end