Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template solutions similar to thymeleaf "natural templates" for jinja2/python/django?

I use jinja2 with google-app-engine and I'm happy with it, it's just that I would like to collaborate with a front-end programmer who does CSS and HTML without forcing him to run the entire stack.

The ideal would be templates that render dummy data instead of the dynamic data when not using the backend, so that a designer can work on the graphics and layout using just the browser and his development environment.

Java has the template engine thymeleaf that can render templates as plain html even though they have tags that takes data from the backend and a framework.

Is there something similar for jinja2, django or python?

like image 401
Niklas Rosencrantz Avatar asked Dec 25 '16 19:12

Niklas Rosencrantz


2 Answers

Template Attribute Language (TAL) & Zope Page Templates (ZPT)

I'm not sure when this came along, but I've been monitoring this space for several years and today was the first time I noticed there is a Python version of TAL, which I did encounter years ago, in PHPTAL, and is very similar to Thymeleaf.

Example code:

<p tal:content="user/getUserName">Placeholder</p>

<table>
  <tr tal:repeat="item context/cart">
    <td tal:content="repeat/item/number">1</td>
    <td tal:content="item/description">Widget</td>
    <td tal:content="item/price">$1.50</td>
  </tr>
</table>

More examples at https://pagetemplates.readthedocs.io/en/latest/tal.html

The reference implementation of TAL in Python can be installed with

pip install zope.pagetemplate

There are also other implementations including a performance-oriented fork and ports to other programming languages.

like image 144
Alex R Avatar answered Oct 11 '22 14:10

Alex R


I would say no, there is no equivalent to thymeleaf in Python. As Alex R mentioned wikipedia lists jina2 as natural templating engine. This is true from a syntactic only perspective, but not for the use-case you have described.

The following jinja2 HTML template is valid HTML:

<!DOCTYPE html>
<html>
  <head>
    <title>{{ variable|escape }}</title>
  </head>
  <body>
    <h1>List of elements</h1>
    <ol>
  {%- for item in item_list %}
      <li>{{ item }}{% if not loop.last %},{% endif %}</li>
  {%- endfor %}
    </ol>
  </body>
</html>

But it will show all the jinja2 parts in the Browser:

List of elements

    {%- for item in item_list %}
  1. {{ item }}{% if not loop.last %},{% endif %}
  2. {%- endfor %}

That may not be as convenient for the front-end developer as the result of the equivalent in thymeleaf:

<!DOCTYPE html>
<html>
  <head>
    <title data-th-text="#{page.title}">Dummy Title</title>
  </head>
  <body>
    <h1>List of elements</h1>
    <ol data-th-each="item : ${items}">
      <li data-th-text="${item}">Dummy Element</li>
    </ol>
  </body>
</html>

Result:

List of elements

  1. Dummy Element
like image 44
lobi Avatar answered Oct 11 '22 14:10

lobi