Open echeran opened 1 year ago
For reference: here are some well-maintained Python templating languages that are mostly HTML but with PHP-like substitutions:
Mako: https://github.com/sqlalchemy/mako
<%inherit file="base.html"/>
<%
rows = [[v for v in range(0,10)] for row in range(0,10)]
%>
<table>
% for row in rows:
${makerow(row)}
% endfor
</table>
<%def name="makerow(row)">
<tr>
% for name in row:
<td>${name}</td>\
% endfor
</tr>
</%def>
Chameleon: https://github.com/malthe/chameleon
<html>
<body>
<h1>Hello, ${'world'}!</h1>
<table>
<tr tal:repeat="row 'apple', 'banana', 'pineapple'">
<td tal:repeat="col 'juice', 'muffin', 'pie'">
${row.capitalize()} ${col}
</td>
</tr>
</table>
</body>
</html>
Genshi: https://github.com/edgewall/genshi
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://genshi.edgewall.org/">
<head>
<title>Geddit: News</title>
</head>
<body class="index">
<div id="header">
<h1>News</h1>
</div>
<ol py:if="links">
<li py:for="link in reversed(links)">
<!-- FAILS HERE: unable to read link object but can read links -->
<a href="${link.url}">${link.title}</a>
posted by ${link.username} at ${link.time.strftime('%x %X')}
</li>
</ol>
<p><a class="action" href="/submit/">Submit new link</a></p>
<div id="footer">
<hr />
<p class="legalese">© 2007 Edgewall Software</p>
</div>
</body>
</html>
Jinja: https://github.com/pallets/jinja/
{% extends "base.html" %}
{% block title %}Members{% endblock %}
{% block content %}
<ul>
{% for user in users %}
<li><a href="{{ user.url }}">{{ user.username }}</a></li>
{% endfor %}
</ul>
{% endblock %}
My opinion: Jinja seems to be the most widely used, and I like the syntax, so probably this is the one to go with if we go with a templating engine.
Created from comment at https://github.com/unicode-org/conformance/pull/67#discussion_r1205999500
+1 from me on this. Doing so should be win-win for everyone. It will probably feel like using jQuery.
It seems like the best way to do this in Python is using the Beautiful Soup library (docs). I've used JSoup in Java before, and that was really nice (powerful and easy). Beautiful Soup and JSoup seem to be comparable.
Using a regular HTML file as the input for HTML templating, rather than some special syntax that requires some special engine to interpret, is a simpler way to go. (Examples of special syntax HTML templating that are all-too-common still: ex1, ex2). The simplicity is that you keep code in Python along with the caller to the library, and you keep markup in HTML, and you don't mix the two. Not having to deal with yet another syntax is a follow on benefit.