20

I am looking for a new Javascript template engine to replace old jQuery Template for my client side templating needs.

I'd prefer approach where the template engine deals with DOM trees instead of text strings and later dumps the content of the cooked string into innerHTML. This is better performance wise and I find DOM manipulation more proper way of constructing more of DOM tree.

What options I do have for Javascript template engine which would directly create DOM trees instead of being text based engines? I like Mustache.js's logicless approach, but it seems to operate on strings only. Native jQuery integration would also be a nice feature.

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • 19
    Thanks. I am looking for a potential solution, not respect =) – Mikko Ohtamaa Feb 14 '12 at 18:43
  • http://blog.nodejitsu.com/micro-templates-are-dead – Mikko Ohtamaa Feb 19 '12 at 13:20
  • 8
    @qwertymk: Get used to it, because it's how everyone will be doing templating in the browser in three years. – Tom Anderson Aug 16 '12 at 17:54
  • imho...in case of generating large templates...I think generating text string would be very fast as compared to subsequent calls to document to create dom elements and adding data/classnames/id's to those elements....or is there any other way these transparency,pure etc do it? – bhavya_w Oct 09 '14 at 10:18
  • As this question is very old (in Javascript-timeline), React.js, from Facebook, has taken some of these concepts and combined them with virtual DOM to very efficient solution https://facebook.github.io/react/ – Mikko Ohtamaa Oct 09 '14 at 10:48
  • 1
    @MikkoOhtamaa sry to bother u again...but react.js, mithril,backbone etc are mvc FRAMEWORKS....I am looking for a templating engine.. – bhavya_w Oct 09 '14 at 12:32
  • React.js might be overkill, but it offers templating, as e.g. presented here http://facebook.github.io/react/docs/tutorial.html - though it goes through higher level component concept. Generally, you are right, rendering pure text templates is faster. However, the speed is not an issue for most real-life applications and DOM-like templating can be done fast as React.js has proven. – Mikko Ohtamaa Oct 09 '14 at 13:08

9 Answers9

12

Transparency:

https://github.com/leonidas/transparency/

PURE:

http://beebole.com/pure/documentation/

Plates

https://github.com/flatiron/plates

Why all this:

http://blog.nodejitsu.com/micro-templates-are-dead

Mikko Ohtamaa
  • 82,057
  • 50
  • 264
  • 435
  • I reviewed all 4 and none of them seem to be actively maintained (am I wrong?). Pure has a recent commit, but it is tiny. Transparency.js hasn't seen a commit in over 7 months (and has tons of open issues). Plates and Weld seemed to be even less active. What do you recommend? – Gili Dec 12 '13 at 19:56
  • Transparency is maintained AFAIK. It is very stable nowadays, so maybe pyykkis, the maintainer, have not considered adding new features. The guy is in IRC and as far as know the project is not dead. – Mikko Ohtamaa Dec 12 '13 at 21:25
  • Note that anyone how finds this answer: This was written a decade ago. Nowadays we have Svelte, React, Vue and Angular. – Mikko Ohtamaa Aug 16 '21 at 09:23
4

Distal

http://code.google.com/p/distal

Kernel James
  • 3,752
  • 25
  • 32
2

soma-template is a new one.

Pure DOM manipulation, a lot of features, natural syntax, fully extensible with other libraries such as underscore.string, function calls with parameters, helpers, watchers. Capability to update only some nodes if needed, templates inside the DOM itself.

http://soundstep.github.com/soma-template/

Soundstep
  • 618
  • 2
  • 6
  • 11
1

This is good comparison about 7 famous JS template engine: http://blog.stevensanderson.com/2012/08/01/rich-javascript-applications-the-seven-frameworks-throne-of-js-2012/

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

I've recently created DOM templating engine inspired by PURE and Transparency.

It supports loops, conditions and much more.

Take a look at doc: http://code.meta-platform.com/metajs/components/template/

Don't be affraid that MetaJS is big library, templating lib can be used standalone.

Short example:

HTML:

<div id="tpl">
    <ul id="tasks">
        <li>
            <span class="task"></span>
            <span class="due-date"></span>
        </li>
    </ul>
</div>

Define template:

var tpl = Meta.Template(document.getElementById('tpl'), {
    "ul#tasks li": $__repeat("tasks", {
        ".task":        "task",
        ".due-date":    $__date("d. m. Y", "due_date"),
        "@":            $__attrIf("completed", "complete")
    })
});

Render template:

tpl({
    tasks: [
        {
            task: "Write concept",
            due_date: new Date(2015, 3, 22, 0, 0, 0, 0),
            complete: true
        }, {
            task: "Consult with customer",
            due_date: new Date(2015, 3, 25, 0, 0, 0, 0),
            complete: false
        }
    ]
});

Result:

<div id="tpl">

    <ul id="tasks">
        <li>
            <span class="task" completed>Write concept</span>
            <span class="due-date">22. 3. 2015</span>
        </li>
        <li>
            <span class="task">Consult with customer</span>
            <span class="due-date">25. 3. 2015</span>
        </li>
    </ul>

</div>
Jiří Hýbek
  • 381
  • 1
  • 9
1

Take a look at Monkberry DOM template engine. Monkberry JavaScript DOM Template Engine

It is really small (just 1,5kB gzipped), dependency free library, server compiling, one-way data binding, and it's dramatically fast!

Here example of template and generated code:

<div>
  <h1>{{ title }}</h1>
  <p>
    {{ text }}
  </p>
</div>

Will generate:

var div = document.createElement('div');
var h1 = document.createElement('h1');
var p = document.createElement('p');

div.appendChild(h1);
div.appendChild(p);

   ...

view.update = function (data) {
  h1.textContent = data.title;
  p.textContent = data.text;
};

Monkberry supports if, for and custom tags. And has a lot of rendering optimizations. Templates can be rendered on server with webpack, browserify or cli.

Anton Medvedev
  • 3,393
  • 3
  • 28
  • 40
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Sandeep Kumar M Jun 30 '15 at 05:43
  • Ok, thanks! BTW docs for monkberry will be available soon. Digg into ;) – Anton Medvedev Jun 30 '15 at 06:44
1

dna.js is a templating engine that clones DOM elements (https://dnajs.org).

Example template for a book:

<h1>Featured Books</h1>
<div id=book class=dna-template>
   <div>Title:  <span>{{title}}</span></div>
   <div>Author: <cite>{{author}}</cite></div>
</div>

Call to insert a copy of the template into the DOM:

dna.clone('book', { title: 'The DOM', author: 'Jan' });

Resulting HTML:

<h1>Featured Books</h1>
<div class=book>
   <div>Title:  <span>The DOM</span></div>
   <div>Author: <cite>Jan</cite></div>
</div>

Fiddle with a sample "To-Do Application":
https://jsfiddle.net/uLrc7kmp

todo

dna.js was created precisely to avoid constructing and passing around string templates (I'm the project maintainer).

Dem Pilafian
  • 5,625
  • 6
  • 39
  • 67
0

Free MIT-licensed jQuery DNA Template with superpowers (you can re-apply the changed data to the same HTML structure to update UI on any data change...)

https://github.com/webdevelopers-eu/jquery-dna-template/

elixon
  • 1,123
  • 12
  • 15
-1

What sort of sugar are you looking for? The raw DOM api always worked fine for me. If you are really adopting this idea that the templating engines are no good in terms of performance, don't use innerHTML if you want to efficiently build up a DOM tree. What I tend to do is just create elements manually using document.createElement. My templates are created by writing helper functions that create collection of nodes and populate them with the data by setting the .innerText property. I can then cache the references to nodes which I wish to refer to frequently so that I don't have to traverse the DOM tree to find these nodes again.

Matt Esch
  • 22,661
  • 8
  • 53
  • 51