UL4 – A templating language
ll.ul4c
provides templating for XML/HTML as well as any other text-based
format. A template defines placeholders for data output and basic logic (like
loops and conditional blocks), that define how the final rendered output will
look.
ll.ul4c
compiles a template to an internal format, which makes it
possible to implement renderers for these templates in multiple programming
languages.
Note
Apart from this Python implementation there are implementations for Java (both a compiler and renderer) and Javascript (renderer only).
In the template source any text surrounded by <?
and ?>
is a “template
tag”. The first word inside the tag is the tag type. It defines what the tag
does. For example <?print foo?>
is a print tag (it prints the value of the
variable foo
). A complete example template looks like this:
<?if data?>
<ul>
<?for item in data?>
<li><?print xmlescape(item)?></li>
<?end for?>
</ul>
<?end if?>
A complete Python program that compiles a template and renders it might look like this:
from ll import ul4c
code = '''
<?if data?>
<ul>
<?for item in data?>
<li><?print item?></li>
<?end for?>
</ul>
<?end if?>
'''
template = ul4c.Template(code)
print(template.renders(data=["Python", "Java", "Javascript", "PHP"]))
The variables that should be available to the template code can be passed to the
method renders()
as keyword arguments.
renders()
returns the final rendered output as a string.
Alternatively the method render()
can be used, which is
writes the output to a text stream.
For more information see the following chapters: