Tmplt.rb Gem Version Build Status Coverage Status

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:

tmpl = "{{ foo }}, {{ bar.baz }}!"
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 a string with tags enclosed in double braces. Use a dot-delimited tag to reference nested values in data.

  • data can be a hash or array, with arbitrary nesting. If a proc in data is referenced in tmpl, it will be invoked, and its return value will be substituted into tmpl. The proc takes a single argument, namely the data object itself.

Installation

Install via RubyGems:

$ gem install tmplt

License

MIT license