Template-Comparison


This page gives the results from some studies of alternative template implementations.

 

Template implementations using the jQuery Templates syntax

The following template implementations all implement effectively the same template syntax, taken from jQuery Templates plugin, and corresponding closely to the grammar documented here: Template.

 

 

jQuery Templates 

This is the jquery.tmpl.js plugin which was made an official plugin, and is now being managed by jQuery UI. It was originally a prototype created by John Resig, and most of the subsequent development was by Boris Moore. The repository is at https://github.com/jquery/jquery-tmpl and the API is documented at http://api.jquery.com/category/plugins/templates/.

 

The API pattern used for rendering data to HTML is: $( "#myTemplate" ).tmpl( myData ).appendTo( "#containerElement" ); where myData can be an object or an array. 

 

Rendering depends on the HTML DOM, and results in a set of rendered nodes. So this is not just string concatenation. One advantage of this is that it is easy (using $.tmplItem) to get the template context of any element in the rendered content. This allows getting to the data, as in var thisDataItem = $.tmplItem( thisElement ).data; as well as accessing the whole template hierarchy, other DOM elements in the same rendered template instance (e.g. Grid row), etc. as illustrated in the following samples: http://jquery.github.com/jquery-tmpl/demos/step-by-step.html.

 

Strappend 

Strappend is Mike Samuel's implementation, which has the goal of re-implementing jQuery Templates as pure string concatenation, seeking improved performance, along with the ability to provide contextually driven automtatic encoding features.

 

See Template for details and description. The code is at https://github.com/mikesamuel/jquery-jquery-tmpl-proposal. It does not provide the DOM integration described above for accessing template context.

 

JsRender and JsViews 

JsRender and JsViews are ongoing work by Boris Moore (working at Microsoft), taking the jquery.tmpl.js design forward with the goal of a) separating jQuery Templates into two components:

 

The code is  at https://github.com/BorisMoore/jsrender and https://github.com/BorisMoore/jsviews (with demos at JsRender and JsViews)

 

Feature comparison

The code for comparing the implementations is at  https://github.com/BorisMoore/template-comparison, with some test pages at Template Comparison Tests.

 

A comparison of the main features is shown in the following table:

 

https://spreadsheets.google.com/spreadsheet/ccc?key=0AusvKVL7jmFUdGZLdGFMaEdBNHVIRVBlaE9yT1J1MlE&hl=en_US

 

 

Features

 

Notes: 

 

Feature differences: Details

Important for Grid - essential features:

Valuable for Grid - desirable features:

Not necessary for Grid - nice-to-have, but not really necessary: 

In the case of the Grid, this feature should not be necessary, however, since the templates in the Grid are built-in, and we, the Grid developers, can guarantee the user of the appropriate encoding for a given context in the template. 

 

Perf comparison

 

The following table provides a selection of perf figures taken from these test pages: http://borismoore.github.com/template-comparison/tests/benchmarks/benchmarks.html.

 

  Chrome  IE9
  No encoding  Encoding  No encoding  Encoding 
  Compile Render Compile Render Compile Render Compile Render
jquery.tmpl 0.083 0.009 0.008 0.022 0.365 0.012 0.008 0.022
JsRender 0.106 0.003 0.106 0.007 0.266 0.004 0.281 0.038
Strappend 0.131 0.006 0.172 0.016 0.284 0.005 0.284 0.011
Strappend-auto 0.331 0.012 0.313 0.019 Not working currently

 

Differences: Details and Notes

Of note is that differences in the rendering performance, once a template is compiled, are fairly insignificant, though jquery.tmpl is slightly slower at encoding, particuarly in IE. (The encoding could be made faster fairly easily, by following the approach Mike Samuel has taken to basic HTML encoding, in Strappend. JsRender figures here already leverage that approach.)

 

On the other hand, compilation is significantly slower with Strappend, because of the more sophisticated pluggable approach which is used there, (and which is needed in order to be able to plug in the contextual encoding provided in Strappend-auto).

 

The question that this therefore raises is whether that compilation-time cost of Strappend is justified by features or other advantages:

 

Conformance Suites

For tests of conformance with jQuery Templates grammar and semantics See conformance suite links from template comparison tests.

 

Examples for template composition

 

MANUAL

<script id="layout-1" type="text/x-jquery-tmpl">

    <body>

        <h1>Title</h1>

        <div id="content">

        </div>

    </body>

</script>

 

<script id="content-1" type="text/x-jquery-tmpl">

    Hello ${username}!

</script>

 

render( "layout-1" ).find( "#content" )

  .append( render( "content-1", { username: "Scott" } );

 

<!-- ######################################## -->

 

WRAPPING

<script id="layout-1" type="text/x-jquery-tmpl">

        <body>

                <h1>Title</h1>

                <div>

                        {{html $child}}

                </div>

        </body>

</script>

 

<script id="content-1" type="text/x-jquery-tmpl">

        {{wrap layout}}

                Hello ${username}!

        {{/wrap}}

</script>

 

render( "content-1", { layout: "layout-1", username: "Scott" } );

 

<!-- ######################################## -->

 

NESTED

<script id="layout-1" type="text/x-jquery-tmpl">

    <body>

        <h1>Title</h1>

        <div>

            {{template $child}}

        </div>

    </body>

</script>

 

<script id="content-1" type="text/x-jquery-tmpl">

    Hello ${username}!

</script>

 

render( "layout-1", { $child: "content-1", username: "Scott" } );