class Rndr::Template

This class is used to handle the rendering of `ERB` templates. @author Bob Killen <rkillen@umich.edu>

Public Class Methods

new(path:, vars: {}) click to toggle source

Constructs a new instance of Template. @param path [String] The path to the template file. @param vars [Hash] A hash of the variables to be passed to the template's binding.

# File lib/rndr/template.rb, line 11
def initialize(path:, vars: {})
  @path = File.absolute_path(path)
  @vars = vars
end

Public Instance Methods

render(render_path) click to toggle source

Renders the template to the supplied path. @param render_path [String] Path to desired rendered template location. @return [Boolean] True if template was rendered successfully.

# File lib/rndr/template.rb, line 25
def render(render_path)
  render_helper(render_path)
end
render?() click to toggle source

Verifies if a template is renderable or not. @return [Boolean] True if template is renderable.

# File lib/rndr/template.rb, line 18
def render?
  render_helper
end

Private Instance Methods

render_helper(render_path = nil) click to toggle source

Does the heavy lifting of template generation. If no path is supplied, it simply tests if the template is renderable. @param render_path [String] Path to desired rendered template location. @return [Boolean] True if template is renderable, and rendered file written.

# File lib/rndr/template.rb, line 35
def render_helper(render_path = nil)
  b = binding
  @vars.each { |k, v| b.local_variable_set(k, v) }
  rendered = ERB.new(File.read(@path), nil, '-').result(b)
  unless render_path.nil?
    File.open(render_path, 'w') { |f| f.write(rendered) }
  end
  true
rescue
  false
end