Skip to content
A template engine for Rails, focusing on speed, using Ruby's String interpolation syntax
Ruby HTML Shell
Branch: master
Clone or download
amatsuda Merge pull request #4 from k0kubun/benchmark-fix
Improve the benchmark script
Latest commit c91c530 Sep 28, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information.
bin bundle gem string_template Dec 21, 2017
lib Let the handler be Rails 6 ready Mar 3, 2019
test Fix Rails 6 errors in StringTemplateTest Sep 12, 2019
.travis.yml CI with the latest rubygems Dec 28, 2017
Gemfile bundle gem string_template Dec 21, 2017
MIT-LICENSE MIT-LICENSE Dec 28, 2017 README Dec 29, 2017
Rakefile bundle gem string_template Dec 21, 2017
benchmark.rb Fix ActionView error on Rails 6 Sep 12, 2019
hello.erb Avoid escaping HTML in hello.erb Sep 12, 2019
hello.string Benchmark vs ERB Dec 28, 2017
string_template.gemspec Version 0.2.1 Mar 3, 2019


The fastest template engine for Rails.


Ruby's String literal has such a powerful interpolation mechanism. It's almost a template engine, it's the fastest way to compose a String, and the syntax is already very well known by every Ruby programmer. Why don't we use this for the view files in our apps?


Add this line to your Rails application's Gemfile:

gem 'string_template'

And then bundle.


StringTemplate's syntax is based on Ruby's String interpolation. Plus, you can use Action View features.


Here's an example of a scaffold generated ERB template, and its string_template version.


<p id="notice"><%= notice %></p>

  <%= @post.title %>

  <%= @post.body %>

<%= link_to 'Edit', "/posts/#{}/edit" %> |
<%= link_to 'Back', '/posts' %>


<p id="notice">#{h notice }</p>

  #{h @post.title }

  #{h @post.body }

#{ link_to 'Edit', "/posts/#{}/edit" } |
#{ link_to 'Back', '/posts' }

More Examples

Please take a look at the tests for actual examples.

File Names

By default, string_template renders view files with .string extension, e.g. app/views/posts/show.html.string


string_template does not automatically html_escape. Don't forget to explicitly call h() when interpolating possibly HTML unsafe strings, like we used to do in pre Rails 3 era.

So, Should We Rewrite Everything with This?

string_template may not be the best choice as a general purpose template engine. It may sometimes be hard to express your template in a simple and maintainable code, especially when the template includes some business logic. You need to care about security. So this template engine is recommended to use only for performance hotspots. For other templates, you might better use your favorite template engine such as haml, or haml, or haml.


Following is the benchmark result showing how string_template is faster than ERB (Erubi, to be technically accurate), executed on Ruby trunk (2.6). This repo includes this actual benchmarking script so that you can try it on your machine.

% ruby benchmark.rb
Warming up --------------------------------------
                 erb   993.525  i/100ms
              string     1.911k i/100ms
Calculating -------------------------------------
                 erb    11.012k i/s -     49.676k in 4.511268s
              string    22.029k i/s -     95.529k in 4.336571s

              string:     22028.7 i/s
                 erb:     11011.5 i/s - 2.00x  slower


Bug reports and pull requests are welcome on GitHub at


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

You can’t perform that action at this time.