Class: Amp::Support::Template
- Inherits:
-
Object
- Object
- Amp::Support::Template
- Defined in:
- lib/amp-core/templates/template.rb
Overview
Template
Class representing a template in the amp system
Direct Known Subclasses
Class Attribute Summary collapse
-
.all_templates ⇒ Object
Returns the value of attribute all_templates.
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#renderer ⇒ Object
Returns the value of attribute renderer.
-
#text ⇒ Object
Returns the value of attribute text.
Class Method Summary collapse
-
.[](type, template) ⇒ Template
Returns the template with the given name.
-
.ensure_templates_loaded ⇒ Object
Makes sure the default templates have been loaded.
-
.load_default_templates ⇒ Object
Registers the default templates.
-
.register(type, name, template) ⇒ Object
Registers a template with the Amp system.
-
.templates_loaded? ⇒ Boolean
Returns whether any templates have been loaded.
-
.unregister(type, name) ⇒ Object
Unregisters a template with the Amp system.
Instance Method Summary collapse
-
#initialize(type, name, renderer = :erb, text = "") ⇒ Template
constructor
Creates a new template with the given values.
-
#render(locals = {}, render_binding = binding) ⇒ String
Renders the template with the given local variables.
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!
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_templates ⇒ Object
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
#name ⇒ Object
Returns the value of attribute name.
91 92 93 |
# File 'lib/amp-core/templates/template.rb', line 91 def name @name end |
#renderer ⇒ Object
Returns the value of attribute renderer.
91 92 93 |
# File 'lib/amp-core/templates/template.rb', line 91 def renderer @renderer end |
#text ⇒ Object
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.
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_loaded ⇒ Object
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_templates ⇒ Object
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.(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.
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.
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.
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
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.
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 |