21

A nice feature of Google's Soy templates is that you can use the same templates on the client (JS) and on the server (Java).

Currently I plan to render most pages client-side using Soy templates compiled to JS. However, my backend is written in Python (using Tornado), so I can't easily use the same templates server-side to generate emails or static pages.

I could render these soy templates using a separate Java component on the server side, or perhaps even get them working in node.js. Neither of those options seem particularly clean.

Are there any good templating engines that run both in JS and Python? Has anyone had good results with JSON-Template or Tenjin? Any other ideas?

Chris Smith
  • 18,244
  • 13
  • 59
  • 81
nickbaum
  • 583
  • 4
  • 11

3 Answers3

11

Mustache is a template engine that has been implemented in both Python and JavaScript (and many other languages).

http://mustache.github.com/

mike
  • 4,393
  • 1
  • 27
  • 16
  • 1
    Thanks for the reply, Mike! Do you have experience using it? How do you find it? Are there any larger sites that use it? – nickbaum Jan 09 '11 at 02:21
  • I maintain a mustache module for a Java MVC framework called Play (https://github.com/murz/play-mustache). Large sites using mustache include Twitter, LinkedIn, Zendesk. There's a list here: https://github.com/janl/mustache.js/wiki/Beard-Competition – mike Jan 10 '11 at 20:25
  • Hey Mike, just noticed this answer. Thanks a lot, really appreciate it! I'm still using Soy for now, but I may try to convert some of our templates to Mustache and see how it works. – nickbaum Jan 18 '11 at 19:11
7

Michael Kerrin has created a project called pwt.jinja2js

Project description:

pwt.jinja2js is an extension to the Jinja2 template engine that compiles valid Jinja2 templates containing macros to JavaScript. The JavaScript output can be included via script tags or can be added to the applications JavaScript.

As stated in the documentation

By slipping a switch we can produce Java Script that takes advantage of Closure Library and produces the following:

Jinja2:

{% namespace ns1 %}

{% macro printusers(users) %}
<ul>
{% for user in users %}
    <li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endmacro %}

Becomes:

goog.provide('ns1');

goog.require('soy');

ns1.printusers = function(opt_data, opt_sb) {
    var output = opt_sb || new soy.StringBuilder();
    output.append('\n<ul>\n');
    var userList = opt_data.users;
    var userListLen = userList.length;
    for (var userIndex = 0; userIndex < userListLen; userIndex++) {
        var userData = userList[userIndex];
   output.append('\n   <li><a href="', userData.url, '">', userData.username, '</a></li>\n');
}
    output.append('\n</ul>\n');
    if (!opt_sb) return output.toString();
}
Kyle Finley
  • 11,842
  • 6
  • 43
  • 64
2

I realize this is a very old question but for the reference, it is now possible to compile Soy templates to Python code.

https://pypi.python.org/pypi/soy

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126