Class: Vagrant::Util::TemplateRenderer

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/vagrant/util/template_renderer.rb

Overview

This class is used to render the ERB templates in the GEM_ROOT/templates directory.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template, data = {}) ⇒ TemplateRenderer

Returns a new instance of TemplateRenderer.



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vagrant/util/template_renderer.rb', line 43

def initialize(template, data = {})
  super()

  @template_root = data.delete(:template_root)
  @template_root ||= Vagrant.source_root.join("templates")
  @template_root = Pathname.new(@template_root)

  data[:template] = template
  data.each do |key, value|
    send("#{key}=", value)
  end
end

Class Method Details

.render(*args) ⇒ String

Render a given template and return the result. This method optionally takes a block which will be passed the renderer prior to rendering, which allows the caller to set any view variables within the renderer itself.

Returns:

  • (String)

    Rendered template



20
21
22
# File 'lib/vagrant/util/template_renderer.rb', line 20

def render(*args)
  render_with(:render, *args)
end

.render_string(*args) ⇒ String

Render a given string and return the result. This method optionally takes a block which will be passed the renderer prior to rendering, which allows the caller to set any view variables within the renderer itself.

Parameters:

  • template (String)

    The template data string.

Returns:

  • (String)

    Rendered template



30
31
32
# File 'lib/vagrant/util/template_renderer.rb', line 30

def render_string(*args)
  render_with(:render_string, *args)
end

.render_with(method, template, data = {}) {|renderer| ... } ⇒ Object

Method used internally to DRY out the other renderers. This method creates and sets up the renderer before calling a specified method on it.

Yields:

  • (renderer)


36
37
38
39
40
# File 'lib/vagrant/util/template_renderer.rb', line 36

def render_with(method, template, data={})
  renderer = new(template, data)
  yield renderer if block_given?
  renderer.send(method.to_sym)
end

Instance Method Details

#full_template_pathString

Returns the full path to the template, taking into account the gem directory and adding the .erb extension to the end.

Returns:

  • (String)


86
87
88
# File 'lib/vagrant/util/template_renderer.rb', line 86

def full_template_path
  @template_root.join("#{template}.erb").to_s.squeeze("/")
end

#renderString

Renders the template using the class instance as the binding. Because the renderer inherits from OpenStruct, additional view variables can be added like normal accessors.

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vagrant/util/template_renderer.rb', line 61

def render
  old_template = template
  result = nil
  File.open(full_template_path, 'r') do |f|
    self.template = f.read
    result = render_string
  end

  result
ensure
  self.template = old_template
end

#render_stringString

Renders a template, handling the template as a string, but otherwise acting the same way as #render.

Returns:

  • (String)


78
79
80
# File 'lib/vagrant/util/template_renderer.rb', line 78

def render_string
  binding.eval(Erubi::Engine.new(template, trim: true).src)
end