Class: Amp::Support::Template

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

Overview

Template

Class representing a template in the amp system

Author:

  • Michael Edgar

Direct Known Subclasses

FileTemplate

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, renderer = :erb, text = "") ⇒ Template

Creates a new template with the given values. The name is how you will reference the template using the –template option from the command line, so choose it wisely!

Parameters:

  • name (String, Symbol, #to_s)

    the name of the template, which is invoked using –template

  • type (Symbol)

    the type of the template. Indicates the renderer used.

  • text (String) (defaults to: "")

    the text of the template, which presumably has some templating code to substitute in local variables and make a nice output system.



86
87
88
89
# File 'lib/amp/templates/template.rb', line 86

def initialize(name, renderer = :erb, text = "")
  @name, @renderer, @text = name, renderer, text
  Template.register(name, self)
end

Class Attribute Details

.all_templatesObject

Returns the value of attribute all_templates.



12
13
14
# File 'lib/amp/templates/template.rb', line 12

def all_templates
  @all_templates
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



75
76
77
# File 'lib/amp/templates/template.rb', line 75

def name
  @name
end

#rendererObject

Returns the value of attribute renderer.



75
76
77
# File 'lib/amp/templates/template.rb', line 75

def renderer
  @renderer
end

#textObject

Returns the value of attribute text.



75
76
77
# File 'lib/amp/templates/template.rb', line 75

def text
  @text
end

Class Method Details

.[](template) ⇒ Template

Returns the template with the given name.

Parameters:

  • template (String, Symbol, #to_sym)

    the name of the template to retrieve

Returns:

  • (Template)

    the template with the given name



19
20
21
22
# File 'lib/amp/templates/template.rb', line 19

def [](template)
  ensure_templates_loaded
  return all_templates[template.to_sym]
end

.ensure_templates_loadedObject

Makes sure the default templates have been loaded.

About the use of instance_eval - this method could potentially be run more than once. There is no reason for it to ever run more than once. So we’ll redefine it to do nothing.



67
68
69
70
71
72
# File 'lib/amp/templates/template.rb', line 67

def ensure_templates_loaded
  load_default_templates
  instance_eval do
    def ensure_templates_loaded; end
  end
end

.load_default_templatesObject

Registers the default templates. Separated into a method (instead of automatically run) because templates aren’t used enough to justify the IO hit from loading them in.



55
56
57
58
59
60
# File 'lib/amp/templates/template.rb', line 55

def load_default_templates
  Dir[File.expand_path(File.join(File.dirname(__FILE__), "*.erb"))].each do |f|
    name = f.split('/').last.chomp('.erb').sub('.','-')
    FileTemplate.new(name, f)
  end
end

.register(name, template) ⇒ Object

Registers a template with the Amp system. Should have a unique name.

Parameters:

  • name (String, Symbol, #to_sym)

    the name of the template. Should be unique.

  • template (Template)

    the template to register.



29
30
31
# File 'lib/amp/templates/template.rb', line 29

def register(name, template)
  all_templates[name.to_sym] = template
end

.templates_loaded?Boolean

Returns whether any templates have been loaded. Used for lazy loading of templates.

Returns:

  • (Boolean)

    have the default templates, or any templates, been loaded?



48
49
50
# File 'lib/amp/templates/template.rb', line 48

def templates_loaded?
  all_templates.any?
end

.unregister(name) ⇒ Object

Unregisters a template with the Amp system. If the name is not found, an exception is thrown.

Parameters:

  • name (String, Symbol, #to_sym)

    the name of the template to remove from the system.

Raises:

  • (ArgumentError)


39
40
41
42
# File 'lib/amp/templates/template.rb', line 39

def unregister(name)
  raise ArgumentError.new("Unknown template: #{name}") unless all_templates[name.to_sym]
  all_templates.delete name.to_sym
end

Instance Method Details

#render(locals = {}, render_binding = binding) ⇒ String

TODO:

make locals work for ERb without bootleg hax

Renders the template with the given local variables. Uses whichever templating engine you set. Note: if you use HAML, you’ll need to have HAML installed. This is why none of the default templates use HAML.

Parameters:

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

    the local variables passed to the template. Works for HAML so far, not for erb.

Returns:

  • (String)

    the parsed template



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/amp/templates/template.rb', line 100

def render(locals = {}, render_binding = binding)
  # expose this local to make it easier, even if it's nil
  
  case renderer.to_sym
  when :erb
    require 'erb'
    locals_assigns = locals.to_a.collect { |k,v| "#{k} = locals[:#{k}]" }
    eval locals_assigns.join("\n"), render_binding
    
    erb = ERB.new(text, 0, "-")
    erb.result render_binding
  when :haml
    require 'rubygems'
    require 'haml'
    haml = Haml::Engine.new(text)
    haml.render render_binding, locals
  end
end