Skip to content

ayanko/rustybars

Repository files navigation

Rustybars

Ruby binding to handlebars-rust

Installation

Install the gem and add to the application's Gemfile by executing:

$ bundle add rustybars

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install rustybars

Usage

Basic

Rustybars::Engine.new.render('Hello {{name}}!', {name: 'John Doe'}.to_json)

Re-use engine

hbs = Rustybars::Engine.new
hbs.render('Hello {{name}}!', {name: 'John Doe'}.to_json)

Re-use compiled template

hbs = Rustybars::Engine.new
template = hbs.compile('Hello {{name}}!')
template.render({name: 'Alice'}.to_json)
# => "Hello Alice!"
template.render({name: 'Bob'}.to_json)
# => "Hello Bob!"

Strict mode

hbs = Rustybars::Engine.new(strict_mode: true)
template = hbs.compile('Hello {{name}}!')
template.render({name: 'Alice'}.to_json)
# => "Hello Alice!"
template.render({another_name: 'Bob'}.to_json)
# =>  Error rendering "Unnamed template" line 1, col 7: Failed to access variable in strict mode Some("name") (Rustybars::RenderError)

Benchmark

$ bundle exec rake bench
                                     user     system      total        real
inline                           0.280195   0.000401   0.280596 (  0.280849)
reuse engine                     0.277403   0.000131   0.277534 (  0.277735)
reuse template                   0.029096   0.000035   0.029131 (  0.029158)
using 10 ruby threads            0.000311   0.000189   0.000500 (  0.000466)

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. 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.

Cross compilation

bundle exec rb-sys-dock -p arm64-darwin --ruby-versions 3.2 --build
bundle exec rb-sys-dock -p x86_64-linux --ruby-versions 3.2 --build

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ayanko/rustybars. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Rustybars project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Ruby binding for handlebars-rust

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published