lxc-config/hooks/lib/lxc/template.rb

26 lines
500 B
Ruby
Raw Normal View History

2015-01-30 08:08:18 +00:00
require "erb"
module Lxc
class TemplateContext < OpenStruct
def get_binding
binding
end
end
class Template
def initialize(path)
@path = path
@erb = ERB.new(File.read(path), nil, "-")
end
def render(params={})
@erb.result(TemplateContext.new(params).get_binding)
rescue => e
raise StandardError.new("fail to render '#{@path}': #{e}")
end
def write(path, options={})
Utils.safe_write(path, render(options))
end
end
end