Dirty is a simple EDSL template library that helps you to write some HTML or XML markup with Python. It is inspired by Markaby.
>>> from dirty.html import *
>>> page = xhtml(
... head(
... title("Dirty"),
... meta(name="Author", content="Hong, MinHee <minhee@dahlia.kr>")
... ),
... body(
... h1("Dirty"),
... p("Dirty is a simple DSEL template library that...")
... )
... )
>>> print(page)
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" />
<head>
<title>Dirty</title>
<meta content="Hong, MinHee <minhee@dahlia.kr>" name="Author" />
</head>
<body>
<h1>Dirty</h1>
<p>Dirty is a simple DSEL template library that...</p>
</body>
</html>Features
- It is a EDSL library for Python 3.0. Dirty is not for large scale HTML/XML documents. Use only for minimal web applications.
- Output is iterable and evaluated lazily. Such behavior is important and useful sometimes e.g. improving slowdown speed, serving big hypertext documents. See also Element.__iter__ method’s docstring.
- Runs only on Python 3.0 or more.
FAQ
How can I loop?
Use a generator expression.
div(
h3("Members"),
ul(li(person.name) for person in members)
)Isn’t there an if-statement in Dirty?
Use a trinary operator.
p("Gender: ", "Male" if person.gender == "M" else "Female")Yes, it’s too dirty. So I named it like that.
I want to print raw XML/HTML strings that are generated by other modules e.g. Markdown.
There is dirty.RawString class.
>>> from dirty import RawString
>>> from dirty.html import *
>>> print(div(RawString("<em>Thank you!</em>")))
<div><em>Thank you!</em></div>How can I use XML namespaces?
Sorry, it is unsupported yet. However, it will be added soon.
How To Install
~ $ wget http://dirty.googlecode.com/files/dirty-1.0.2.tar.gz ~ $ tar xvfz dirty-1.0.2.tar.gz ~ $ cd dirty-1.0.2 ~/dirty-1.0.2 $ python3.0 setup.py install