Git Product home page Git Product logo

string_template's Introduction

StringTemplate

The fastest template engine for Rails.

Concept

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?

Installation

Add this line to your Rails application's Gemfile:

gem 'string_template'

And then bundle.

Syntax

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

Example

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

ERB:

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

<p>
  <strong>Title:</strong>
  <%= @post.title %>
</p>

<p>
  <strong>Body:</strong>
  <%= @post.body %>
</p>

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

string_template:

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

<p>
  <strong>Title:</strong>
  #{h @post.title }
</p>

<p>
  <strong>Body:</strong>
  #{h @post.body }
</p>

#{ link_to 'Edit', "/posts/#{@post.id}/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

Security

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.

Benchmark

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

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

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/amatsuda/string_template.

License

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

string_template's People

Contributors

amatsuda avatar mame avatar

Watchers

James Cloos avatar Muhammad Yana Mulyana avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.