class Lita::Template
A simple wrapper around ERB to render text from files or strings. @since 4.2.0
Attributes
erb[R]
The underlying ERB object.
helpers[RW]
Public Class Methods
from_file(path)
click to toggle source
new(source)
click to toggle source
@param source [String] A string to use as the template's content.
# File lib/lita/template.rb, line 25 def initialize(source) @erb = ERB.new(source, $SAFE, "<>") self.helpers = Set.new end
Public Instance Methods
add_helper(helper)
click to toggle source
Add a module of helpers methods to be added to the template evalutation context. @param helper [Module] The module to extend onto the template evalutation context. @return [void] @since 4.5.0
# File lib/lita/template.rb, line 34 def add_helper(helper) helpers << helper end
render(variables = {})
click to toggle source
Render the template with the provided variables. @param variables [Hash] A collection of variables for interpolation. Each key-value pair will
make the value available inside the template as an instance variable with the key as its name.
# File lib/lita/template.rb, line 42 def render(variables = {}) erb.result(context_binding(variables)) end
Private Instance Methods
context_binding(variables)
click to toggle source
Create an empty object to use as the ERB context and set any provided variables in it.
# File lib/lita/template.rb, line 51 def context_binding(variables) context = TemplateEvaluationContext.new helpers.each { |helper| context.extend(helper) } variables.each do |k, v| context.instance_variable_set("@#{k}", v) end context.__get_binding end