All Projects → amatsuda → String_template

amatsuda / String_template

Licence: mit
A template engine for Rails, focusing on speed, using Ruby's String interpolation syntax

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to String template

Email Templates
📫 Create, preview, and send custom email templates for Node.js. Highly configurable and supports automatic inline CSS, stylesheets, embedded images and fonts, and much more!
Stars: ✭ 3,291 (+2597.54%)
Mutual labels:  template, template-engine
Suspenders
A Rails template with our standard defaults, ready to deploy to Heroku.
Stars: ✭ 3,748 (+2972.13%)
Mutual labels:  rails, template
Mikado
Mikado is the webs fastest template library for building user interfaces.
Stars: ✭ 323 (+164.75%)
Mutual labels:  template, template-engine
The construct
A Modern Rails Template
Stars: ✭ 183 (+50%)
Mutual labels:  rails, template
Jb
A simple and fast JSON API template engine for Ruby on Rails
Stars: ✭ 1,075 (+781.15%)
Mutual labels:  rails, template-engine
Himl
HTML-based Indented Markup Language for Ruby
Stars: ✭ 236 (+93.44%)
Mutual labels:  rails, template-engine
Rails Template
A best & newest & fastest rails 6.x template for senior rails developer.
Stars: ✭ 372 (+204.92%)
Mutual labels:  rails, template
Pongo2
Django-syntax like template-engine for Go
Stars: ✭ 2,111 (+1630.33%)
Mutual labels:  template, template-engine
Raygun Rails
Rails 6 application template for Raygun, the Carbon Five Rails application generator.
Stars: ✭ 48 (-60.66%)
Mutual labels:  rails, template
Phug
Phug - The Pug Template Engine for PHP
Stars: ✭ 43 (-64.75%)
Mutual labels:  template, template-engine
Jade
Jade.go - pug template engine for Go (golang)
Stars: ✭ 251 (+105.74%)
Mutual labels:  template, template-engine
Tuxedo
Tuxedo is a template language for Swift.
Stars: ✭ 80 (-34.43%)
Mutual labels:  template, template-engine
Sodajs
Light weight but powerful template engine for JavaScript
Stars: ✭ 240 (+96.72%)
Mutual labels:  template, template-engine
Generate
A new command line tool and developer framework for scaffolding out GitHub projects. Generate offers the robustness and configurability of Yeoman, the expressiveness and simplicity of Slush, and more powerful flow control and composability than either.
Stars: ✭ 238 (+95.08%)
Mutual labels:  rails, template
Windowstemplatestudio
Windows Template Studio quickly builds a UWP app, using a wizard-based UI to turn your needs into a foundation of Windows 10 patterns and best practices.
Stars: ✭ 2,089 (+1612.3%)
Mutual labels:  template, template-engine
Pug
Pug template engine for PHP
Stars: ✭ 341 (+179.51%)
Mutual labels:  template, template-engine
Westwind.razorhosting
Hosting the Razor Runtime outside of ASP.NET/MVC for use in non-Web .NET applications.
Stars: ✭ 136 (+11.48%)
Mutual labels:  template, template-engine
Inja
A Template Engine for Modern C++
Stars: ✭ 715 (+486.07%)
Mutual labels:  template, template-engine
Liquor
Liquor is a safe sandboxing compiling template language for Ruby
Stars: ✭ 57 (-53.28%)
Mutual labels:  template, template-engine
Rails docker template
Docker template for Rails app or Rails + Webpacker app development.
Stars: ✭ 82 (-32.79%)
Mutual labels:  rails, template

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].