Class: Milkshake::Template
- Inherits:
-
Object
- Object
- Milkshake::Template
- Defined in:
- lib/milkshake/template.rb
Defined Under Namespace
Modules: SourceFile
Class Attribute Summary collapse
-
.template_dir ⇒ Object
Returns the value of attribute template_dir.
Class Method Summary collapse
Instance Method Summary collapse
- #evaluate(context = {}) ⇒ Object
-
#initialize(template) ⇒ Template
constructor
A new instance of Template.
Constructor Details
#initialize(template) ⇒ Template
Returns a new instance of Template.
27 28 29 |
# File 'lib/milkshake/template.rb', line 27 def initialize(template) @template = template end |
Class Attribute Details
.template_dir ⇒ Object
Returns the value of attribute template_dir.
6 7 8 |
# File 'lib/milkshake/template.rb', line 6 def template_dir @template_dir end |
Class Method Details
.[](name) ⇒ Object
8 9 10 |
# File 'lib/milkshake/template.rb', line 8 def [](name) (@templates ||= {})[name] ||= load(name) end |
.evaluate(name, context = {}) ⇒ Object
12 13 14 |
# File 'lib/milkshake/template.rb', line 12 def evaluate(name, context={}) self[name].evaluate(context) end |
.load(name) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/milkshake/template.rb', line 18 def self.load(name) path = File.join(self.template_dir, name) if File.exist?(path) new IO.read(path) else raise "Missing template #{name}" end end |
Instance Method Details
#evaluate(context = {}) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/milkshake/template.rb', line 31 def evaluate(context={}) result = @template.dup result.gsub!(/\{\{([a-zA-Z0-9_.-]+)\}\}/) do |match| (context[$1] || context[$1.to_sym]).to_s end result.extend SourceFile result end |