views:

4640

answers:

14

I'm kind of falling in love with Node.js not because you write app code in javascript but because of its performance.

I really don't care a lot about how beautiful a server side language might be but how much requests per second it can handle.

So anyway I'm looking forward to experiment building an entire webapp using Node.js (and going back to the actual question) is there a template engine similar to let's say the django template engine or something similar (that at least allows you to extend base templates) available for Node.js?

+2  A: 

This articles covers alot, but node.js is pretty fresh. http://simonwillison.net/2009/Nov/23/node/

Sveisvei
+4  A: 

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
+12  A: 

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
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.
felixge
Do any of these support inheritance?
Nick Retallack
+3  A: 

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
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
+9  A: 

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
There is also a blog post about using Mustache and Underscore together with Node.js: http://boldr.net/create-a-web-app-with-node
MKroehnert
+2  A: 

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/

Orange Box
+6  A: 

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
+3  A: 

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
+3  A: 

Have you tried haml based templating engine support in express framework: http://expressjs.com/intro.html

+1  A: 

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  A: 

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

tjholowaychuk
A: 

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
A: 

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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;head&gt;&lt;title&gt;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">[email protected]
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>
Mark Lindell
+3  A: 

Hi,

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

Cheers

Ramesh Vel