Class: Docify::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/docify/template.rb

Constant Summary collapse

REGEX =
/(\{\{([a-z\-\_]{1,})\}\})/i

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ Template

Initialize Template object with a string template

Raises:

  • (ArgumentError)


6
7
8
9
# File 'lib/docify/template.rb', line 6

def initialize(content)
  @template = content.strip.to_s
  raise ArgumentError, "Template content required!" if @template.empty?
end

Instance Method Details

#render(params = {}) ⇒ Object

Render template

params - Hash of parameters.
         Each params in template should be wrapped with {{var}}.


14
15
16
17
18
19
20
# File 'lib/docify/template.rb', line 14

def render(params={})
  params.stringify_keys!
  @template.gsub(REGEX) do |m|
    key = m.scan(REGEX).flatten.last.to_s
    m = params[key]
  end
end