Insider
A simple and elegant Ruby gem for string template interpolation with variable substitution.
Features
- Simple template engine with
{{variable}}syntax - Lightweight and dependency-free
- Easy-to-use API for quick template rendering
- Support for multiple variable substitutions
Installation
Add this line to your application's Gemfile:
gem 'insider'And then execute:
bundle installOr install it yourself as:
gem install insiderUsage
# first and always
require 'insider'
# 1. By using including the module within a class
class MyClass
include Insider
attr_reader :message
def initialize name
@name = name
# 2. creating an insider instance
@message = insider("my_class_id")
end
def hello!
# 3. the template can be set
@message.template = %["Hello, {{name}}!"]
# 4. and arguments applied
@message.apply(name: @name)
# 5. anywhere!
end
end
# then, by creating a class instance
@x = MyClass.new("World")
# our insider can work.
@x.hello! => "Hello, World!"
### AND / OR ###
# Use the module directly
# 1. create an instance
@template = Insider[:my_template]
# 2. set the template
@template.template = "Hello, {{name}}!"
# 3. apply arguments
@template.apply(name: "World") => "Hello, World!"API Reference
Insider::Template
Methods
-
initialize(id)- Creates a new template with the given identifier -
template- Getter/setter for the template string -
apply(variables = {})- Applies variable substitution to the template
Insider Module
-
Insider[id]- Creates a new template instance with the given ID -
insider(name)- Instance method to create a new template
Development
After checking out the repo, run bin/setup to install dependencies. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/insider.
License
The gem is available as open source under the terms of the MIT License.