Class: Natsuzora::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/natsuzora/template.rb

Overview

Top-level entry that compiles a template source and renders against a Payload.

‘Template#render` accepts only a Payload, which is the explicit boundary between untrusted host data and the internal value space. Callers wishing to render from a raw Hash should either use the convenience facade render or wrap the Hash in `Natsuzora::Payload.new(…)` themselves.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, include_root: nil) ⇒ Template

Returns a new instance of Template.



15
16
17
18
19
# File 'lib/natsuzora/template.rb', line 15

def initialize(source, include_root: nil)
  @source = source
  @loader = include_root && TemplateLoader.new(include_root)
  @ast = parse_ruby(source)
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



13
14
15
# File 'lib/natsuzora/template.rb', line 13

def ast
  @ast
end

Instance Method Details

#render(payload) ⇒ String

Returns rendered output.

Parameters:

Returns:

  • (String)

    rendered output



23
24
25
# File 'lib/natsuzora/template.rb', line 23

def render(payload)
  Renderer.new(@ast, template_loader: @loader).render(payload.data)
end