Module: Cloudkeeper::One::ApplianceActions::Utils::TemplatePreparation

Included in:
Registration, Cloudkeeper::One::ApplianceActions::Update
Defined in:
lib/cloudkeeper/one/appliance_actions/utils/template_preparation.rb

Instance Method Summary collapse

Instance Method Details

#prepare_template(filename, data) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/cloudkeeper/one/appliance_actions/utils/template_preparation.rb', line 9

def prepare_template(filename, data)
  template_file = File.join(Cloudkeeper::One::Settings[:'appliances-template-dir'], filename)
  raise Cloudkeeper::One::Errors::ArgumentError, "Missing file #{filename.inspect} in template directory" \
    unless File.exist?(template_file)

  logger.debug "Populating template from #{template_file}"
  templates = [template_file, File.join(File.dirname(__FILE__), 'templates', 'attributes.erb')]

  data[:image] ||= nil
  rendered = render_templates templates, data
  logger.debug "Template:\n#{rendered}"
  rendered
end

#render_templates(templates, data) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/cloudkeeper/one/appliance_actions/utils/template_preparation.rb', line 23

def render_templates(templates, data)
  Tempfile.open 'cloudkeeper-template' do |tmp|
    templates.each { |template| tmp.write(File.read(template)) }
    tmp.flush

    template = Tilt::ERBTemplate.new tmp
    template.render Object.new, data
  end
end