Class: Nake::ErbTemplate

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

Instance Method Summary (collapse)

Constructor Details

- (ErbTemplate) initialize(path)



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

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

Instance Method Details

- (Object) render(context = Hash.new)



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

- (Object) set_locals(context)



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

- (Object) source(context)



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

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