Is there a template engine for Node.js?
JavascriptTemplate Enginenode.jsJavascript Problem Overview
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?
Javascript Solutions
Solution 1 - Javascript
Check out the Node js modules wiki page. They have listed all the templating engines supporting node.js.
Solution 2 - Javascript
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.
Solution 3 - Javascript
If you like haml, but want something even better check out http://jade-lang.com for node, I wrote haml.js as well :)
Solution 4 - Javascript
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
Solution 5 - Javascript
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>
Solution 6 - Javascript
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>
Solution 7 - Javascript
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.
Solution 8 - Javascript
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.
Solution 9 - Javascript
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.
Solution 10 - Javascript
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.
Solution 11 - Javascript
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">[email protected]
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>
Solution 12 - Javascript
I've heard good things about {dust} http://akdubya.github.com/dustjs/#dust
Solution 13 - Javascript
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);
Solution 14 - Javascript
Google's Closure Templates is a natively-JavaScript templating system and a seemingly natural fit with NodeJS. Here are some instructions for integrating them.
Solution 15 - Javascript
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.
Solution 16 - Javascript
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.
Solution 17 - Javascript
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.
Solution 18 - Javascript
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...
Solution 19 - Javascript
Here's a good evaluation of several engines http://engineering.linkedin.com/frontend/client-side-templating-throwdown-mustache-handlebars-dustjs-and-more
Solution 20 - Javascript
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).
Solution 21 - Javascript
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.