class Okay::Template

An extremely simple templating engine.

General usage:

template = Okay::Template.new("./templates")
puts template.apply("some_template.html", {"some_key": "some value"})
template.directory #=> "./templates"

Attributes

directory[R]

Public Class Methods

new(directory) click to toggle source

Create an Okay::Templates object.

@param directory [String] Path of the directory containing the templates.

# File lib/okay/template.rb, line 25
def initialize(directory)
  @directory = directory
end

Public Instance Methods

apply(template_name, data) click to toggle source

Apply the template referenced by template_name to data.

@param template_name [String] Name of the template to use,

relative to +@directory+ (as passed to +#initialize+).

@param data [Hash] Data to apply the template to.

@return [String] Result of applying the template to data.

# File lib/okay/template.rb, line 37
def apply(template_name, data)
  template_file = Pathname.new(@directory).join(template_name)
  template = template_file.read

  # Silence warnings while applying the template, since we don't
  # generally care about unused keys.
  silence_warnings { Kernel.format(template, data) }
end