Class: Inprovise::Template

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

Instance Method Summary collapse

Constructor Details

#initialize(path, context = nil) ⇒ Template

Returns a new instance of Template.



11
12
13
14
15
# File 'lib/inprovise/template.rb', line 11

def initialize(path, context = nil)
  @context = context || Object.new
  @path = resolve(path)
  @template = @path.respond_to?(:call) ? Tilt['erb'].new(&@path) : Tilt.new(@path)
end

Instance Method Details

#render(locals = {}) ⇒ Object



17
18
19
# File 'lib/inprovise/template.rb', line 17

def render(locals={})
  @template.render(@context, locals)
end

#render_to_tempfile(locals = {}) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/inprovise/template.rb', line 21

def render_to_tempfile(locals={})
  basename = @path.respond_to?(:call) ? 'inprovise-inline-tpl' : File.basename(@path).gsub('.', '-')
  file = Tempfile.new(basename)
  file.write render(locals)
  file.close
  file.path
end