Is there a template engine for Node.js? [closed]
Asked Answered
M

21

274

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?

Mixedup answered 24/11, 2009 at 4:8 Comment(13)
I 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.Wriggle
@Nosredna: How can Java possibly be faster than anything?! ;)Desensitize
@Daniel Java is actually pretty fast these days, beating everything but Ada, C and C++ in Debian's Shootout benchmarks.Pragmaticism
Anyone who doesn't care about syntax, productivity and anything else but performance, should be using Raphters (web framework for C)Scrivner
node.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.Slackjawed
@DanielSloof Javascript not Java...Electrotherapy
@korri: read the whole comment thread :)Desensitize
@DanielSloof woops, my bad !Electrotherapy
If you really don't mind about the syntax or "beauty" of a language but performance only, I hear assembler is pretty fast ;)Precision
@Precision You might find this answer interesting :) #2684864Illicit
@grasGendarme I was being sarcastic dude.Precision
you can try jade jade-lang.comErst
npm install templetize - Full syntax extensible engine, works like a charm!Stratiform
V
166

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

Venenose answered 8/9, 2010 at 6:44 Comment(2)
The page became deprecated you can find an alternative page on Visionmedia's wiki and the chapter about templates.Comatulid
@ThomasPotaire It became deprecated because they suggest to use npm search instead.Jutland
A
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.

Antevert answered 17/12, 2009 at 18:56 Comment(2)
There is also a blog post about using Mustache and Underscore together with Node.js: boldr.net/create-a-web-app-with-nodeCedilla
@Cedilla the link is brokenMaraud
H
39

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

Hansom answered 2/7, 2010 at 15:10 Comment(2)
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.Charlatanry
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 / concatsHansom
W
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

Wriggle answered 24/11, 2009 at 5:56 Comment(3)
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 (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.Justitia
Do any of these support inheritance?Amasa
@Nick jinjs seems to support inheritance, see the first line: github.com/ravelsoft/node-jinjs/blob/master/test/templates/…Phantasm
S
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>
Szczecin answered 3/1, 2010 at 18:5 Comment(0)
F
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>
Fourteenth answered 21/10, 2012 at 10:43 Comment(2)
Please be more descriptive in your answer about your code and the link you referred to. Refer:How to AnswerUturn
Would be interesting, how to actually process the file or populate variables within NodeSalpa
C
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.

Cheadle answered 11/3, 2010 at 0:59 Comment(0)
M
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.

Marylnmarylou answered 2/7, 2012 at 10:9 Comment(3)
TwigJS is kind of dead, GitHub says last commit was2years ago. Swig, on he other hand, is pretty acive. I'd go for Swig.Rhizogenic
FYI: As of this commit on Jun 25th 2015, the Swig github claims it is no longer being maintained. That might change in the future, but as of now, it's something to consider.Anlace
Nunjucks has very similar syntax, for what it's worth.Senatorial
A
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.

Anstus answered 24/11, 2009 at 5:52 Comment(1)
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.Niu
A
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.

Ayrshire answered 11/8, 2011 at 12:25 Comment(0)
E
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">[email protected]
</div><div id="bio">Experienced software professional...
</div></div></div></body></html>
Evensong answered 13/8, 2010 at 20:48 Comment(0)
S
4

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

Sacculus answered 28/3, 2011 at 21:47 Comment(0)
B
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);
Budge answered 18/10, 2011 at 21:35 Comment(0)
O
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.

Overman answered 9/7, 2010 at 16:49 Comment(0)
G
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.

Grisby answered 5/12, 2009 at 20:54 Comment(1)
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.Grisby
M
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/

Marsiella answered 20/12, 2009 at 20:30 Comment(0)
Z
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.

Zavala answered 31/5, 2010 at 6:47 Comment(1)
Interesting, nice work mishoo. Does it have any advantage over EJS?Nealneala
B
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...

Bettyannbettye answered 19/2, 2012 at 2:54 Comment(0)
G
0

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

Goldston answered 12/3, 2012 at 6:46 Comment(0)
N
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).

Nealneala answered 26/8, 2012 at 11:42 Comment(0)
H
-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.

Hudspeth answered 26/9, 2011 at 8:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.