Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Templating system for both Python and Javascript?

Tags:

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?

like image 406
nickbaum Avatar asked Jan 04 '11 07:01

nickbaum


People also ask

Can you use Python and JavaScript together?

You can use Python and its modules inside JavaScript with Promise API. You can test it with your favorite python modules such as Numpy, Pandas, pyautogui etc at this point or other built in modules if you want.

Is JavaScript a templating language?

JavaScript templating refers to the client side data binding method implemented with the JavaScript language. This approach became popular thanks to JavaScript's increased use, its increase in client processing capabilities, and the trend to outsource computations to the client's web browser.

What is use of templating in Python?

Templating in Python. Templating, and in particular web templating is a way to represent data in different forms. These forms often (but not always) intended to be readable, even attractive, to a human audience. Frequently, templating solutions involve a document (the template) and data.


2 Answers

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

http://mustache.github.com/

like image 118
mike Avatar answered Oct 25 '22 16:10

mike


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();
}
like image 23
Kyle Finley Avatar answered Oct 25 '22 16:10

Kyle Finley