Class: Boxes::Template

Inherits:
Object
  • Object
show all
Includes:
Errors
Defined in:
lib/boxes/template.rb

Overview

Representations of Packer templates.

Defined Under Namespace

Classes: ERBContext

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, name) ⇒ Boxes::Template

Load a template with a given name.

Parameters:

  • env (Boxes::Environment)

    the environment to source templates.

  • name (String)

    the name of the template.



14
15
16
17
18
19
20
21
22
# File 'lib/boxes/template.rb', line 14

def initialize(env, name)
  fail(TemplateNotFoundError) unless env.available_templates.include?(name)

  @name = name
  @template = ''
  File.open(Boxes.config.working_dir + "templates/#{name}.erb") do |f|
    @template << f.read
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/boxes/template.rb', line 6

def name
  @name
end

#templateObject (readonly)

Returns the value of attribute template.



6
7
8
# File 'lib/boxes/template.rb', line 6

def template
  @template
end

Instance Method Details

#render(args) ⇒ String

Render the template.

Parameters:

  • args (Hash)

    the values to set.

Returns:

  • (String)

    the rendered template.



29
30
31
# File 'lib/boxes/template.rb', line 29

def render(args)
  ERB.new(template, nil, '-').result(ERBContext.new(args).get_binding)
end