Class: NanoTemplate

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

Overview

text template like erb

Defined Under Namespace

Classes: TemplateCompileError

Instance Method Summary collapse

Instance Method Details

#template(value) ⇒ Proc

build template

Examples:

NanoTemplate.new.template(“<%= foo %>bar”).call(OpenStruct.new({ foo: 42 })) # 42bar


Parameters:

  • value (String)

    template string

Returns:

  • (Proc)

    compiled template Proc


8
9
10
11
12
13
14
15
16
17
# File 'lib/nano_template.rb', line 8

def template(value)
  begin
    sub = build_template(value)
    Proc.new do |stash|
      stash.instance_eval &sub
    end
  rescue => error
    raise "template compile error:\n#{error}\n--\n#{value}"
  end
end