class Effigie::Template
The Effigie::Template
class provides provides some utilities to read an ERB template and render it within ruby standard library
Attributes
filepath[R]
Public Class Methods
new(filepath = nil)
click to toggle source
Creates a new instance of Effigie::Template
Params:
filepath
-
string
a 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"))
# File lib/effigie/template.rb, line 32 def initialize(filepath = nil) @filepath = filepath end
Public Instance Methods
render(ctx)
click to toggle source
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
# File lib/effigie/template.rb, line 54 def render(ctx) erb.result(ctx.instance_eval { binding }) end
Private Instance Methods
erb()
click to toggle source
# File lib/effigie/template.rb, line 60 def erb @erb ||= ERB.new(read) end
read()
click to toggle source
# File lib/effigie/template.rb, line 64 def read @read ||= File.read(filepath) end