Class: Effigie::Template
- Inherits:
-
Object
- Object
- Effigie::Template
- Defined in:
- lib/effigie/template.rb
Overview
The Effigie::Template class provides provides some utilities to read an ERB template and render it within ruby standard library
Instance Attribute Summary collapse
-
#filepath ⇒ Object
readonly
Returns the value of attribute filepath.
Instance Method Summary collapse
-
#initialize(filepath = nil) ⇒ Template
constructor
Creates a new instance of
Effigie::Template. -
#render(ctx) ⇒ Object
It renders your template out of the
Bindinginstance of the object passed as argument.
Constructor Details
#initialize(filepath = nil) ⇒ Template
Creates a new instance of Effigie::Template
Params:
filepath-
stringa file path
In case you do not want to read from file You should override erb private method
Usage:
Reading from file path +Effigie::Template.new(“path/to/file.erb”)
Overriding erb method
class HelloWorldTemplate < Effigie::Template
def erb
ERB.new("Hello <%= name %>")
end
end
HelloWorldTemplate.new.render(OpenStruct.new(name: "World"))
32 33 34 |
# File 'lib/effigie/template.rb', line 32 def initialize(filepath = nil) @filepath = filepath end |
Instance Attribute Details
#filepath ⇒ Object (readonly)
Returns the value of attribute filepath.
7 8 9 |
# File 'lib/effigie/template.rb', line 7 def filepath @filepath end |
Instance Method Details
#render(ctx) ⇒ Object
It renders your template out of the Binding instance of the object passed as argument
Params:
ctx-
any object
E.g. Usage with Hash
class HashTemplate < Effigie::Template
def erb
ERB.new("Hello <%= self[:name] %>")
end
end
HashTemplate.new.render(name: "World")
Please see the tests for further examples
54 55 56 |
# File 'lib/effigie/template.rb', line 54 def render(ctx) erb.result(ctx.instance_eval { binding }) end |