Zeus
Python HTML templating DSL.
Installation
$ pip install zeus
Usage
from zeus import html, p
# Currently, Zeus supports two operators for working with HTML elements:
# << (left bitwise shift) adds attributes from given dictionary
# >> (right bitwise shift) adds inner content from given iterable of elements
# all operations have same semantics: {target} {operator} {data}
(html >> (p >> "{w}".format(w=w) for w in ["hello", "world"]))
# => <html><p>hello</p><p>world</p></html>
# also, its possible to chain zeus methods, for example
(p >> "good ol' plain text data") << {"id": "paragraph"}
# => <p id="paragraph">good ol' plain text data</p>
# this line can be described as this:
# a. create element p
# b. fill its content with string
# c. add #id attribute to it
# and methods call order doesn't matter:
p << {"id": "paragraph"} >> "good ol' plain text data"
# => <p id="paragraph">good ol' plain text data</p>
Web page example
from zeus import html, head, title, body, div, h3
articles = [
{
"title": "The Ballot as a Substitute for Brains",
},
{
"title": "Test À La Hamlet",
},
]
print(html >> (
head >> (
title >> "Liberty, Benjamin Tucker"
),
body >> (
div << {"id": "articles"} >> (
h3 >> article.get("title") for article in articles
)
),
))