A Ruby gem for interpolating values from a hash or array into a template string.
Usage
Straight-up substitution, nothing more. Here’s a slightly contrived example:
require 'tmplt'
tmpl = "{{ foo }}, {{ baz.qux }}!"
data = {
:foo => "Hello",
:bar => "World",
:baz => {
:qux => Proc.new {|d| "#{d[:bar]}" }
}
}
puts Tmplt.render(tmpl, data) #=> "Hello, World!"
More usage examples are in the tests.
API
Tmplt.render(tmpl, data)
Interpolates values from data
into the tmpl
string.
-
tmpl
is astring
with tags enclosed in double braces. Use a dot-delimited tag to reference nested values indata
. -
data
can be ahash
orarray
, with arbitrary nesting. If aproc
indata
is referenced intmpl
, it will be invoked, and its return value will be substituted intotmpl
. Theproc
takes a single argument, namely thedata
object itself.
Installation
Install via RubyGems:
$ gem install tmplt