Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ruby Hybrid Template Engine for Client Side and Server Side, EJS-Based

branch: master
README.md

Isotope - Ruby Hybrid Template Engine for Client Side and Server Side

The problem:

In Ajax-based sites, there's a constant dilemma: How to get objects rendered in templates? In server side (and output full HTML)? Client side (and mess with JSON objects and HTML strings/DOM generation)? What should be the role division between server and client?

Common Approaches, Pros & Cons:

A few approaches to output a rendered template evaluated with an object are:

Approach #1: Regular ERB Partial

Evaluate a simple ERB partial with a local object, and server it as a string to the client, simply by

<%= render :partial => "article", :object => @article %>

in a view, or from a controller and request it by Ajax.

This partial can look like:

<h2><%= article.title %></h2>
<div class="content">
<%= article.content %>
</div>
<ul class="tags">
    <% article.tags.each { |tag| %>
    <li><%= tag.name %></li>
    <% } %>
</ul>

Pros

  • Simple, readable and well known ERB for Rails/Sinatra
  • SEO and accessibility - HTML code is downloaded into the source

Cons

  • Server side only
  • Requires download of the whole HTML code, can cause performance issues
  • Cannot bind easily to a different object on client side. Must re-rendered in the server-side and be downloaded

Approach #2: Client Side EJS Template with JSON Objects

Having an EJS template in the HTML code, with techniques such as John Resig's JavaScript Micro-Templating

<script type="text/html" id="article-template">
<h2><%= item.title %></h2>

<div class="content">
<%= item.content %>
</div>

<ul class="tags">
    <% item.tags.forEach(function (tag) { %>
    <li><%= tag.name %></li>
    <% }); %>
</ul>
</script>

Query the server for a JSON article and evaluate the template with this object into a string, and place it inside a container, using a technique as mentioned:

var results = document.getElementById("results"); // some container on the page
results.innerHTML = tmpl("article-template", article); // article is an object, probably a result of an AJAX JSON request

Pros

  • Fast - requires the server to send only the JSON object and the HTML is downloaded only once as a template

Cons

  • SEO and accessibility - HTML code isn't in the source of the page but being rendered after load

Approach #3: Regular ERB Partial With Marked Spots for Data Place Holders

This approach tries to combine server side and client side but requires a lot of work. It contains a regular ERB template and place holder markers like class names on elements. The template can be first evaluated on the server with a Ruby object and on the client side it can be evaluated with a different JS object (probably from a JSON request).

Template file should look like:

<h2 class="data-title"><%= article.title %></h2>
<div class="content data-content">
<%= article.content %>
</div>
<ul class="tags data-tags">
    <% article.tags.each { |tag| %>
    <li><%= tag.name %></li>
    <% } %>
</ul>

And then, from a JS function, doing something like:

// article is an object probably from a JSON request
container.querySelector('.data-title').textContent=article.title;
container.querySelector('.data-content').textContent=article.content;
var tags=container.querySelector('.data-tags');
tags.innerHTML="";
article.tags.forEach(function (tag) {
    tags.appendChild(document.createElement("li")).textContent=tag.name;
});

Pros

  • One template for both client and server (not really, see Cons)
  • SEO and accessibility - HTML code is downloaded into the source

Cons

  • On the client side - Must mimic the Ruby functionality with JS when it comes to loops, conditions etc. However, text values are pretty easy to embed. This code should probably written manually for everything that is not a simple textual content.
  • Must maintain data place holder markers

So...

In these three approaches, the developer needs to choose according to the task and the project requirements, or worse, maintain two templates, ERB and EJS.

Each approach is written in a totally different way, and switching between the approaches means a lot of work.

Introducing: Isotope - Ruby Hybrid Template Engine for Client Side and Server Side

So why not combining all the pros of the approaches together?

The biggest constraints to be considered are:

  • Client side doesn't understand Ruby
  • Ruby can't be translated fully into JavaScript
  • And the most important one: Template should be maintained in one single file for both client and server uses

Isotope is from greek - "Equal Place". An equal place of editing a template for both client and server (Thanks @yuvalraz for the name!).

Using jbarnette's AWESOME Johnson gem, Ruby and JavaScript can interact together!

That means, that ruby code can handle EJS templates and JSON objects. A great and very inspiring article is Write your Rails view in… JavaScript? by Aaron Patterson.

In this approach, only one template is written and maintained in an EJS format, for both client side and server side.

Usage

Isotope gives the ability to have a single template file, and easily switch between the approaches:

# article.html.ejs

<h2><%= item.title %></h2>

<div class="content">
<%= item.content %>
</div>

<ul class="tags">
    <% item.tags.forEach(function (tag) { %>
    <li><%= tag.name %></li>
    <% }); %>
</ul>

On the Client Side

Outputting from the server side (controller or view)

<%= isotope_render_template("articles/article", :id => "article-template") %>

The above code will output:

<script type="text/x-isotope" id="article-template">
<h2><%= item.title %></h2>

<div class="content">
<%= item.content %>
</div>

<ul class="tags">
    <% item.tags.forEach(function (tag) { %>
    <li><%= tag.name %></li>
    <% }); %>
</ul>
</script>

which is easy to evaluate with any JS object using the mentioned technique.

On the Server Side (The Holy Grail)

Using Johnson, the famous micro-templating technique and JSONed Ruby objects, this library provides the following functionality:

<%= isotope_render_partial("articles/article", :locals => { :item => @article }) %>

This code reads the source of the EJS file, uses Johnson and John Resig's technique and serves a string as an output.

Configuration

External JS files can be included in order to have special functionality in the EJS templates, on both client and server. To include files, create a config/isotope.yml file and fill this array, relatively to the app root:

include_scripts:
    - /public/javascripts/isotope_functions.js

And in order to have it available on client side, put this line before the

Something went wrong with that request. Please try again.