Class: SimpleTemplate

Inherits:
Object
  • Object
show all
Defined in:
app/models/simple_template.rb

Class Method Summary collapse

Class Method Details

.[](str, variables = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/simple_template.rb', line 3

def [](str, variables = {})
  return "" if str.nil?
  variables ||= {}

  str.gsub /\{\{([^\}]*)\}\}/ do |matched|
    key = $1.strip.to_sym

    if variables.include? key
      value = variables[key]
      value = value.call if value.respond_to? :call
      value
    elsif variables.include?(:self) && variables[:self].respond_to?(key)
      variables[:self].send key
    else
      ""
    end
  end
end