TinyTemplate
This is a very simple template engine that allows you to interpolate method chains without passing it any data. TinyTemplate will use the self
of the caller when no context is passed.
Installation
Add this line to your application's Gemfile:
gem 'tiny_template'
And then execute:
$ bundle
Or install it yourself as:
$ gem install tiny_template
Usage
You just have to prepend your strings with the ~ operator to parse them with the methods of the current self :
class Greeting
def hello
~"Hello {{client.name.upcase}} and welcome to {{configuration.title.capitalize}}.
Your email is {{client.email.downcase.strip}}."
end
def client
OpenStruct.new(name: 'John Doe', email: 'John@doe.com')
end
def configuration
OpenStruct.new(title: 'my fancy website')
end
end
Greeting.new.hello
# => "Hello JOHN DOE and welcome to My fancy website. Your email is john@doe.com."
If you don't like the binding magic used here, you can use TinyTemplate with the classic syntax :
template = "Hello {{client.name.upcase}} and welcome to {{configuration.title.capitalize}}.
Your email is {{client.email.downcase.strip}}."
TinyTemplate.parse(template, self)
If your interpreted string comes from a non-trusted source (user inpur for instance), you can secure it by providing a white list of method chains.
TinyTemplate.secure(['client.name', 'configuration.email']) do
~my_template
TinyTemplate.parse(my_template)
end
Contributing
- Fork it ( https://github.com/[my-github-username]/tiny_template/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request