Class: Amp::Support::Template

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

Overview

Template

Class representing a template in the amp system

Author:

  • Michael Edgar

Direct Known Subclasses

FileTemplate, RawERbTemplate

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, 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.



102
103
104
105
# File 'lib/amp-core/templates/template.rb', line 102

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

Class Attribute Details

.all_templatesObject

Returns the value of attribute all_templates.



26
27
28
# File 'lib/amp-core/templates/template.rb', line 26

def all_templates
  @all_templates
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



91
92
93
# File 'lib/amp-core/templates/template.rb', line 91

def name
  @name
end

#rendererObject

Returns the value of attribute renderer.



91
92
93
# File 'lib/amp-core/templates/template.rb', line 91

def renderer
  @renderer
end

#textObject

Returns the value of attribute text.



91
92
93
# File 'lib/amp-core/templates/template.rb', line 91

def text
  @text
end

Class Method Details

.[](type, 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



33
34
35
36
# File 'lib/amp-core/templates/template.rb', line 33

def [](type, template)
  ensure_templates_loaded
  return all_templates[type.to_sym][template.to_sym] || all_templates[:all][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.



82
83
84
85
86
87
88
# File 'lib/amp-core/templates/template.rb', line 82

def ensure_templates_loaded
  load_default_templates
  instance_eval do
    def ensure_templates_loaded; end
  end
  true
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.



69
70
71
72
73
74
75
# File 'lib/amp-core/templates/template.rb', line 69

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

.register(type, 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.



43
44
45
# File 'lib/amp-core/templates/template.rb', line 43

def register(type, name, template)
  all_templates[type.to_sym][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?



62
63
64
# File 'lib/amp-core/templates/template.rb', line 62

def templates_loaded?
  all_templates.any?
end

.unregister(type, 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)


53
54
55
56
# File 'lib/amp-core/templates/template.rb', line 53

def unregister(type, name)
  raise ArgumentError.new("Unknown template: #{name}") unless all_templates[type.to_sym][name.to_sym]
  all_templates[type.to_sym].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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/amp-core/templates/template.rb', line 116

def render(locals = {}, render_binding = binding)        
  case renderer.to_sym
  when :erb
    require 'erb'
    locals_assigns = locals.map { |k,v| "#{k} = locals[#{k.inspect}]" }
    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