Class: Lita::Template
- Inherits:
-
Object
- Object
- Lita::Template
- Defined in:
- lib/lita/template.rb
Overview
A simple wrapper around ERB to render text from files or strings.
Defined Under Namespace
Classes: TemplateEvaluationContext
Class Method Summary collapse
-
.from_file(path) ⇒ Object
Initializes a new Template with the contents of the file at the given path.
Instance Method Summary collapse
-
#add_helper(helper) ⇒ void
Add a module of helpers methods to be added to the template evalutation context.
-
#initialize(source) ⇒ Template
constructor
A new instance of Template.
-
#render(variables = {}) ⇒ Object
Render the template with the provided variables.
Constructor Details
#initialize(source) ⇒ Template
Returns a new instance of Template.
25 26 27 28 |
# File 'lib/lita/template.rb', line 25 def initialize(source) @erb = ERB.new(source, trim_mode: "<>") self.helpers = Set.new end |
Class Method Details
.from_file(path) ⇒ Object
Initializes a new Template with the contents of the file at the given path.
19 20 21 |
# File 'lib/lita/template.rb', line 19 def from_file(path) new(File.read(path).chomp) end |
Instance Method Details
#add_helper(helper) ⇒ void
This method returns an undefined value.
Add a module of helpers methods to be added to the template evalutation context.
34 35 36 |
# File 'lib/lita/template.rb', line 34 def add_helper(helper) helpers << helper end |
#render(variables = {}) ⇒ Object
Render the template with the provided variables.
42 43 44 |
# File 'lib/lita/template.rb', line 42 def render(variables = {}) erb.result(context_binding(variables)) end |