272

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?

user229044
  • 232,980
  • 40
  • 330
  • 338
Seb
  • 3,059
  • 3
  • 18
  • 6

21 Answers21

166

Check out the Node js modules wiki page. They have listed all the templating engines supporting node.js.

tzi
  • 8,719
  • 2
  • 25
  • 45
RameshVel
  • 64,778
  • 30
  • 169
  • 213
  • 9
    The 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
53

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.

mikeal
  • 4,037
  • 2
  • 27
  • 22
40

If you like haml, but want something even better check out http://jade-lang.com for node, I wrote haml.js as well :)

tjholowaychuk
  • 409
  • 4
  • 2
  • 4
    Jade 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
  • 3
    the 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
14

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

Nosredna
  • 83,000
  • 15
  • 95
  • 122
  • 6
    Thumbs 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
    Do any of these support inheritance? – Nick Retallack Jan 04 '10 at 05:55
  • 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
13

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>
Fabian Jakobs
  • 28,815
  • 8
  • 42
  • 39
7

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>
Ry-
  • 218,210
  • 55
  • 464
  • 476
carambolage
  • 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
6

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.

Tac Tacelosky
  • 3,165
  • 3
  • 27
  • 28
  • 2
    TwigJS 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
6

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.

AHM
  • 5,145
  • 34
  • 37
5

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.

ShZ
  • 6,518
  • 1
  • 26
  • 22
  • 2
    It'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
5

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.

fe_lix_
  • 938
  • 11
  • 18
4

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>
Mark Lindell
  • 852
  • 8
  • 19
4

I've heard good things about {dust} http://akdubya.github.com/dustjs/#dust

triptych
  • 170
  • 3
  • 6
4

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);
HoLyVieR
  • 10,985
  • 5
  • 42
  • 67
ramr
  • 49
  • 1
3

Google's Closure Templates is a natively-JavaScript templating system and a seemingly natural fit with NodeJS. Here are some instructions for integrating them.

Adam Crossland
  • 14,198
  • 3
  • 44
  • 54
2

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.

Mic
  • 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
2

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.

http://code.google.com/p/jtl-javascript-template/

2

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.

mishoo
  • 2,415
  • 1
  • 18
  • 12
0

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).

trusktr
  • 44,284
  • 53
  • 191
  • 263
0

I found hogan.js from Twitter and recommended by Tim O'Reilly on his site. I have no best practice with it, but I trust on Twitter and O'Reilly. You should try...

KimKha
  • 4,370
  • 1
  • 37
  • 45
0

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

Marius
  • 1,659
  • 3
  • 22
  • 31
-1

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.

supNate
  • 432
  • 3
  • 10