views:

2013

answers:

13

Do you have experience with a Javascript templating engine, one that is stable, easy to use and has good performance?

I need to do apply the same template many times for different data. I prefer to download the template itself once (and have it cached) rather than processing the template on the server. Also, this way the template itself would be a static resource more easily cached in the server side too.

+7  A: 

I've been looking at John Resig's JavaScript Micro Templating thingie, and it looks very interesting. Looks like it's simple, does the job, and is very easy to use.

As far as caching is concerned, it's just a matter of setting the right response headers in your web server, no?

Rakesh Pai
"it's just a matter of setting the right response headers in your web server, no?" We have "global" tags like {{ContactTelephoneNo}} which are substituted ServerSide (and cached) rather than sending the data to Client for template-merge. Cached Template marked stale when ContactTelephoneNo changes
Kristen
Do you mean that you want to ensure that the result of a template substitution should be retained in cache until such time that values in the substitution change? That should be a fairly straightforward thing to bolt on to Resig's templating engine.
Rakesh Pai
+3  A: 

Check out PURE javascript templating engine. It's quite easy to use and reasonably light weight.

trex279
+2  A: 

Pure Javascript with JSP-like syntax:

http://blog.markturansky.com/BetterJavascriptTemplates.html

Mark
Hey Mark, I love what you did. I put your code on github and modified it to accept a string as an input and return the parsed output:http://github.com/superjoe30/jst-parserYou can use a HEREDOC in javascript like this:http://mook.wordpress.com/2005/10/30/multi-line-strings-in-javascript/I'm working on a script that helps you use this in Django.
superjoe30
A: 

If you are operating in a Java/JEE environment, you might want to check out dctemplate

Joe
A: 

Tenjin brags about being fast, although it looks to be implemented the same way Mark's is. Also documentation is in broken English and he thinks you'll want to hook up the javascript to Rhino or Java or something.

Mark's solution is great, but I disagree about the API. I modified it and put it on github. I came up with a way to integrate it into Django using preparsed javascript. I documented everything but haven't posted it online. If anyone pokes me I'll write an article on how to set it up.

superjoe30
A: 

Yajet is a new one, spotting a syntax different from anything we've seen before. :-) It compiles the templates and it's blazing fast. It's browser and library-agnostic; there is a small jQuery wrapper for people who can't live without jQuery, but the engine itself is independent and can run in Rhino or V8 too.

It supports many directives that allow conditionals, loops, define reusable template components etc.

mishoo
+1  A: 

Microsoft has proposed a very nice jQuery templating engine:

http://wiki.github.com/nje/jquery/jquery-templates-proposal

Philippe Leybaert
+1  A: 

I like jQote. http://aefxx.com/jquery-plugins/jqote2/

Check out the benchmarks

Flemish Bee Cycle
+2  A: 

I found PURE initally appealing, but hard to get into, inflexible and a wee bit too contrived.

jQote is clean, simple, powerful, recently updated, and apparently, quick.

Dent Arthur Dent
Ok for the "hard to get into", as it is a very different approach than other templating engines. But what do you mean by inflexible and too contrived?
Mic
+1  A: 

+1 for Resig's Micro templating thingy, as mentioned by Rakesh Pai, but thought I'd mention another micro template language on the block: mustache.js

RyanWilcox
A: 

I looked at a few of these solutions, and though nice ideas, I highly recommend not to introduce another library/framework unless you really have to.

I ended up using a simple (yet effective) solution, that may fit your needs too:

templates = {
     contact_details : function( data ){
                             return "<p>" +
                                    "Name:  " + data.name + "<br>" +
                                    "Phone: " + data.phone + "<br>" +
                                    "email: " + data.email " +
                                    "</p>";
}

and then you simply use (with jQuery) as:

$(this).append( templates.contact_details(p) );

or for adding multiple instances (using jQuery's each):

var output = '';
$.each( persons, function(p){
           output+= templates.contact_details(p);
});

you get:
1. Total control of what's happening (no "black box") to debug later.
2. if you have decent IDE - since all templates are under one object you get code completion of your available templates!

quick, and simple.

yanayz
Please don't do this. A template scheme should, at a minimum, help escape values for you. Otherwise you're just asking for XSS vulnerabilities.
Mike Samuel
A: 

Trimpath

morgancodes
A: 

I used a lot of engine. But all of them have the advantage of disadvantage. I think I made myself the most ideal. http://github.com/ismail-codar/templatejs It's parses your javascript syntaxed template, generates javascript and evaluate it. Very easy and usefull.

ismail codar