I'm experimenting with building an entire web application using Node.js. Is there a template engine similar to (for example) the Django template engine or the like that at least allows you to extend base templates?
-
17I just found out that JavaScript V8 engine is faster than Ruby, PHP, and Python. Amazingly fast for a dynamic language. Slower than Java and C#, though. – Nosredna Nov 24 '09 at 19:10
-
77@Nosredna: How can Java possibly be faster than anything?! ;) – Daniel Sloof Sep 08 '10 at 07:06
-
27@Daniel Java is actually pretty fast these days, beating everything but Ada, C and C++ in Debian's Shootout benchmarks. – Mentalikryst Oct 19 '10 at 20:49
-
20Anyone who doesn't care about syntax, productivity and anything else but performance, should be using [Raphters](https://github.com/DanielWaterworth/raphters) (web framework for C) – Pablo B. Apr 17 '11 at 00:27
-
6node.js is not about the language. It's totally about the design and the way js handles IO, which is brilliant. You could make ruby web frameworks run fast if you redesigned it's IO libraries. – julx Apr 25 '11 at 20:11
-
@DanielSloof Javascript not Java... – Korri Dec 14 '12 at 20:06
-
@korri: read the whole comment thread :) – Daniel Sloof Dec 14 '12 at 23:29
-
@DanielSloof woops, my bad ! – Korri Dec 21 '12 at 16:39
-
3If you really don't mind about the syntax or "beauty" of a language but performance only, I hear assembler is pretty fast ;) – arg20 Mar 10 '13 at 05:45
-
2@arg20 You might find this answer interesting :) http://stackoverflow.com/questions/2684364/why-arent-programs-written-in-assembly-more-often/2685541#2685541 – toasted_flakes May 11 '13 at 21:25
-
@grasGendarme I was being sarcastic dude. – arg20 May 12 '13 at 21:53
-
you can try jade http://jade-lang.com/ – Fizer Khan Feb 17 '14 at 06:07
-
1npm install templetize - Full syntax extensible engine, works like a charm! – Lex Podgorny Feb 22 '14 at 03:12
21 Answers
Check out the Node js modules wiki page. They have listed all the templating engines supporting node.js.
-
9The page became deprecated you can find an alternative page on [Visionmedia's wiki](https://github.com/visionmedia/express/wiki) and [the chapter about templates](https://github.com/visionmedia/express/wiki#wiki-template-engines). – Thomas Potaire Mar 01 '14 at 01:44
-
@ThomasPotaire It became deprecated because they suggest to use npm search instead. – inf3rno Jun 12 '15 at 14:47
You should be able to use mustache.js, if it doesn't work send me the issues and I'll get it fixed because I'm about to be using them in node.js anyway.
http://github.com/janl/mustache.js
I know that it works without a DOM because a bunch of CouchDB standalone apps are using it in a Spidermonkey view server.

- 4,037
- 2
- 27
- 22
-
3There is also a blog post about using Mustache and Underscore together with Node.js: http://boldr.net/create-a-web-app-with-node – MKroehnert Apr 25 '10 at 23:53
-
If you like haml, but want something even better check out http://jade-lang.com for node, I wrote haml.js as well :)

- 409
- 4
- 2
-
4Jade is pretty cool. I've just started using it, so can't speak to it's performance, but I like the syntax. And built-in support in Express is a plus. – broofa Nov 22 '10 at 13:41
-
3the performance is sufficient, templates should be cached anyway. Plus remember to scale horizontally, not vertically, otherwise you might as well not use a template engine at all and just some functions / concats – tjholowaychuk Jan 28 '11 at 20:10
There are new templating engines all the time.
underscore.js adds a lot of functional programming support to js, and has templating.
And just today I heard about this: http://github.com/SamuraiJack/Shotenjin-Joosed

- 83,000
- 15
- 95
- 122
-
6Thumbs up for underscore.js. Awesome library, I use it both for my client side as well as node.js work. Their templating engine is based on John Resig's JS Micro Templating engine (http://ejohn.org/blog/javascript-micro-templating/) which I have used many times before. Afaik its your best option at this point when working with node.js. – Felix Geisendörfer Nov 28 '09 at 11:36
-
2
-
1@Nick jinjs seems to support inheritance, see the first line: https://github.com/ravelsoft/node-jinjs/blob/master/test/templates/test-extend.tpl – panchicore Sep 16 '11 at 14:11
You should take a look at node-asyncEJS, which is explicitly designed to take the asynchronous nature of node.js into account. It even allows async code blocks inside of the template.
Here an example form the documentation:
<html>
<head>
<% ctx.hello = "World"; %>
<title><%= "Hello " + ctx.hello %></title>
</head>
<body>
<h1><%? setTimeout(function () { res.print("Async Header"); res.finish(); }, 2000) %></h1>
<p><%? setTimeout(function () { res.print("Body"); res.finish(); }, 1000) %></p>
</body>
</html>

- 28,815
- 8
- 42
- 39
You can try beardless (it's inspired by weld/plates):
For example:
{ post:
{ title: "Next generation templating: Start shaving!"
, text: "TL;DR You should really check out beardless!"
, comments:
[ {text: "Hey cool!"}
, {text: "Really gotta check that out..."} ]
}
}
Your template:
<h1 data-template="post.title"></h1>
<p data-template="post.text"></p>
<div>
<div data-template="post.comments" class="comment">
<p data-template="post.comments.text"></p>
</div>
</div>
Output:
<h1>Next generation templating: Start shaving!</h1>
<p>TL;DR You should really check out beardless!</p>
<div>
<div class="comment">
<p>Hey cool!</p>
</div>
<div class="comment">
<p>Really gotta check that out...</p>
</div>
</div>

- 218,210
- 55
- 464
- 476

- 71
- 1
- 1
-
Please be more descriptive in your answer about your code and the link you referred to. Refer:[How to Answer](http://stackoverflow.com/questions/how-to-answer) – askmish Oct 21 '12 at 15:39
-
Would be interesting, how to actually process the file or populate variables within Node – Julian F. Weinert Jun 13 '15 at 12:09
I use Twig with Symfony and am now dabbling in node.js, so I'm looking at https://github.com/justjohn/twig.js and https://github.com/paularmstrong/swig, which you'll probably like if you use django.

- 3,165
- 3
- 27
- 28
-
2TwigJS is kind of dead, GitHub says last commit was2years ago. Swig, on he other hand, is pretty acive. I'd go for Swig. – Darkhogg May 10 '13 at 08:21
-
**FYI**: As of [this commit on Jun 25th 2015](https://github.com/paularmstrong/swig/commit/20b20e72d6a3a0a73f5c322142aff555be5bd707), the [Swig github claims it is no longer being maintained](https://github.com/paularmstrong/swig/issues/628). That might change in the future, but as of now, it's something to consider. – radiovisual Aug 17 '15 at 12:39
-
[Nunjucks](https://mozilla.github.io/nunjucks/) has very similar syntax, for what it's worth. – mwcz May 15 '19 at 20:36
I have done some work on a pretty complete port of the Django template language for Simon Willisons djangode project (Utilities functions for node.js that borrow some useful concepts from Django).
See the documentation here.

- 5,145
- 34
- 37
If you're looking for a minimalist approach to templates, you can check out JSON Template.
A more full-featured alternative is EJS. It's a bit more similar to something you'd get from Django.
Your mileage may vary for each of these - they're designed for a browser Javascript environment, and not Node.js.

- 6,518
- 1
- 26
- 22
-
2It's not similar to Django's templates at all, for one Django's templates have *good* documentation. EJS is a struggle to get through and you'll end up reading its source code just to understand what's available to you. – Jan 20 '14 at 04:27
WARNING : JinJs is not maintained anymore. It is still working but not compatible with the lastest version of express.
You could try using jinjs. It is a port of the Jinja, a very good Python templating system. You can install it with npm like this :
npm install jinjs
in template.tpl :
I say : "{{ sentence }}"
in your template.js :
jinjs = require('jinjs');
jinjs.registerExtension('.tpl');
tpl = require('./template');
str = tpl.render ({sentence : 'Hello, World!'});
console.log(str);
The output will be :
I say : "Hello, World!"
We are actively developing it, a good documentation should come pretty soon.

- 938
- 11
- 18
haml is a good choice for node.js
http://github.com/creationix/haml-js
haml-js
!!! XML
!!! strict
%html{ xmlns: "http://www.w3.org/1999/xhtml" }
%head
%title Sample haml template
%body
.profile
.left.column
#date= print_date()
#address= current_user.address
.right.column
#email= current_user.email
#bio= current_user.bio
html
<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>Sample haml template
</title></head><body><div class="profile"><div class="left column"><div id="date">January 1, 2009
</div><div id="address">Richardson, TX
</div></div><div class="right column"><div id="email">tim@creationix.com
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>

- 852
- 8
- 19
Try "vash" - asp.net mvc like razor syntax for node.js
https://github.com/kirbysayshi/Vash
also checkout: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx
// sample
var tmpl = vash.compile('<hr/>@model.a,@model.b<hr/>');
var html = tmpl({"a": "hello", "b": "world"});
res.write(html);
Google's Closure Templates is a natively-JavaScript templating system and a seemingly natural fit with NodeJS. Here are some instructions for integrating them.

- 14,198
- 3
- 44
- 54
Did you try PURE ?
If you give it a try, feel free to post any trouble you may face at the forum
While it was primarly designed for the browser, it works well with Jaxer and Rhino.
I don't know node.js yet but if you can cache some JS and functions in memory, the speed should be even more impressive.

- 24,812
- 9
- 57
- 70
-
Node.js does not understand DOM... PURE use the DOM, but since it makes strings of it. This is interesting to investigate. Sorry for the noise. – Mic Dec 05 '09 at 22:05
There is a port of the Django templating engine to JavaScript. However, its not been updated for a long time but it may still have enough features.
Try Yajet too. ;-) It's a new one that I just released yesterday, but I'm using it for a while now and it's stable and fast (templates are compiled to a native JS function).
It has IMO the best syntax possible for a template engine, and a rich feature set despite its small code size (8.5K minified). It has directives that allow you to introduce conditionals, iterate arrays/hashes, define reusable template components etc.

- 2,415
- 1
- 18
- 12
Honestly, the best and most simple template engine for Node.js is (IMHO) Plates (https://github.com/flatiron/plates). You might also want to check out the Flatiron MVC framework for Node.js (http://flatiron.org).

- 44,284
- 53
- 191
- 263
Here's a good evaluation of several engines http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more

- 1,659
- 3
- 22
- 31
You can use dojox.dtl of DojoToolkit.org. Note that dojo 1.7 can well run on NodeJS and perform as a server side library. If you're interested, I can give you a simple example.

- 432
- 3
- 10