Class: Foresite::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/foresite/renderer.rb

Overview

Renderer class.

Basic implementation of ERB for a path to a given template and variables.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, vars) ⇒ Renderer

Constructor.

Parameters:

  • path (String)

    Path to file.

  • vars (Hash)

    Variables for template.



13
14
15
16
17
18
19
20
21
# File 'lib/foresite/renderer.rb', line 13

def initialize(path, vars)
  @path = path
  @vars_original = @vars
  vars.each do |k, v|
    if k.is_a?(Symbol)
      instance_variable_set(:"@#{k}", v)
    end
  end
end

Class Method Details

.render(path, vars) ⇒ String

Statically renders template with variables.

Parameters:

  • path (String)

    Path to file.

  • vars (Hash)

    Variables for template.

Returns:

  • (String)

    Rendered template output.



40
41
42
# File 'lib/foresite/renderer.rb', line 40

def self.render(path, vars)
  new(path, vars).render
end

Instance Method Details

#renderString

Renders template with variables.

Returns:

  • (String)

    Rendered template output.



28
29
30
# File 'lib/foresite/renderer.rb', line 28

def render
  ::ERB.new(File.read(@path), trim_mode: "-").result(binding)
end