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

26 lines
500 B
Ruby

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