Class: ZTK::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/ztk/template.rb

Overview

Erubis Templating Class

Given a template like this (i.e. "template.erb"):

This is a test template!
<%= @variable %>

We can do this:

ZTK::Template.render("template.erb", { :variable => "Hello World" })

And get:

This is a test template!
Hello World

Author:

Class Method Summary (collapse)

Class Method Details

+ (String) do_not_edit_notice(options = {})

Renders a "DO NOT EDIT" notice for placement in generated files.

Parameters:

  • options (Hash) (defaults to: {})

    Options hash.

Options Hash (options):

  • :message (String)

    An optional message to display in the notice.

  • :char (String)

    The comment character; defaults to '#'.

Returns:

  • (String)

    The rendered noticed.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ztk/template.rb', line 58

def do_not_edit_notice(options={})
  message = options[:message]
  char    = (options[:char] || '#')
  notice  = Array.new

  notice << char
  notice << "#{char} WARNING: AUTOMATICALLY GENERATED FILE; DO NOT EDIT!"
  if !message.nil?
    notice << char
    notice << "#{char} #{message}"
  end
  notice << char
  notice << "#{char} Generated @ #{Time.now.utc}"
  notice << char

  notice.join("\n") + "\n"
end

+ (String) render(template, context = nil)

Renders a template to a string.

Parameters:

  • template (String)

    The ERB template to process.

  • context (Hash) (defaults to: nil)

    A hash containing key-pairs, for which the keys are turned into global variables with their respective values.

Returns:

  • (String)

    The evaulated template content.



37
38
39
# File 'lib/ztk/template.rb', line 37

def render(template, context=nil)
  render_template(load_template(template), context)
end

+ (String) string(template, context = nil)

Renders a string to a string.

Parameters:

  • template (String)

    The ERB template to process.

  • context (Hash) (defaults to: nil)

    A hash containing key-pairs, for which the keys are turned into global variables with their respective values.

Returns:

  • (String)

    The evaulated template content.



47
48
49
# File 'lib/ztk/template.rb', line 47

def string(template, context=nil)
  render_template(template, context)
end