2015-01-30 08:08:18 +00:00
|
|
|
require "erb"
|
|
|
|
|
|
|
|
module Lxc
|
|
|
|
class TemplateContext < OpenStruct
|
|
|
|
def get_binding
|
|
|
|
binding
|
|
|
|
end
|
2015-08-05 15:33:37 +00:00
|
|
|
def fqdn(v)
|
|
|
|
v.to_s.gsub(/[^a-zA-Z0-9\-]/, "-")
|
|
|
|
end
|
2015-01-30 08:08:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class Template
|
2015-03-30 22:02:32 +00:00
|
|
|
def initialize(path, context: nil)
|
2015-01-30 08:08:18 +00:00
|
|
|
@path = path
|
|
|
|
@erb = ERB.new(File.read(path), nil, "-")
|
|
|
|
end
|
|
|
|
def render(params={})
|
2015-03-30 22:02:32 +00:00
|
|
|
context = TemplateContext.new(params)
|
|
|
|
@erb.result(context.get_binding)
|
2015-01-30 08:08:18 +00:00
|
|
|
rescue => e
|
|
|
|
raise StandardError.new("fail to render '#{@path}': #{e}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def write(path, options={})
|
|
|
|
Utils.safe_write(path, render(options))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|