Class: Nake::ErbTemplate

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

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ ErbTemplate

Returns a new instance of ErbTemplate.



31
32
33
34
# File 'lib/nake/template.rb', line 31

def initialize(path)
  require "erb"
  @path = path
end

Instance Method Details

#render(context = Hash.new) ⇒ Object



46
47
48
49
# File 'lib/nake/template.rb', line 46

def render(context = Hash.new)
  template = ERB.new(self.source(context))
  template.result(binding)
end

#set_locals(context) ⇒ Object



36
37
38
39
40
# File 'lib/nake/template.rb', line 36

def set_locals(context)
  context.inject("") do |source, pair|
    source += "#{pair.first} = context[:#{pair.first}]\n"
  end
end

#source(context) ⇒ Object



42
43
44
# File 'lib/nake/template.rb', line 42

def source(context)
  ["<% #{self.set_locals(context)} %>", File.read(@path)].join("")
end