Kid 0.5 is available and I think I finally like it enough to start
dumping out some example templates and API usage.
Let's just throw everything at you at once:
<?xml version='1.0' encoding='utf-8'?>
<?python
# python blocks can be embedded anywhere in a template
# but cannot output anything directly..
# import some modules
import os, time
# define some functions, too
def percent_of(iz, ove):
return (iz / ove)
# bind some names..
title = "Hello World"
fruits = ["apple", "orange", "kiwi", "M&M"]
ts = time.gmtime()
?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:py="http://naeblis.cx/ns/kid#">
<head>
<!--
curly braces can be used to interpolate python expressions in
text items and attribute values.
-->
<title>naeblis.cx - {title}</title>
<meta name="generator"
value="kid template v{kid.__version__}" />
</head>
<body>
<!--
you can also use design-tool friendly attributes
to evaluate expressions for element content.
-->
<h1 py:content="title.upper()">
This text is replaced with the uppercased title..
</h1>
<!--
full python expressions let you express yourself fully. :)
-->
<p>We're {round(percent_of(ts.tm_yday, 365.0) * 100, 2)}%
through the year.</p>
<!--
iterate over sequences. the li element is repeated
for each item in the list of fruits
-->
<ul>
<li py:for="fruit in fruits">
I like {fruit}s..
</li>
</ul>
<!-- control output conditionally -->
<p py:if="ts.tm_wday == 0">
Somebody's got the case of the <em>Muundays</em>.. :(
</p>
<!--
define template functions that can be imported and
called from other templates
-->
<table py:def="display_dict(name_title, val_title, mapping)">
<tr><th>{name_title}</th><th>{val_title}</th></tr>
<tr py:for="name, val in mapping.values()">
<td>{name}</td>
<td>{val}</td>
</tr>
</table>
</body>
</html>
The last bit of this template defines a template function, which is a normal
python function that generates XML content. Kid templates can be used like
normal python modules so let's say we called the previous example
example1.kid
and right next to it we have the following in example2.kid
:
<?xml version="1.0" encoding="utf-8"?>
<?python
# you can treat templates just like python modules
from example1 import display_dict
lil_bears = { 'PHP' : 'Too soft',
'XSLT' : 'Too hard',
'Kid' : 'Just right!' }
?>
<html xmlns:py="http://naeblis.cx/ns/kid#">
<head>
<title>Let's use that template function</title>
</head>
<body>
<h1>Here's a table of stuff</h1>
{display_dict('Language', 'Rating', lil_bears)}
</body>
</html>
The result is that the display_dict()
function is called and all generated
XML information items are pulled into the calling template at the point the
function is called. This results in a two column / three row table being
output using the values of the lil_bears
mapping we defined earlier.
We can also do some interesting things by passing values from python into the
template. In the first example, we bound the name title
to the value
"Hello World". We can override module level bindings like that by passing
names into the template like so:
import example1
result = example1.serialize(title="Goodbye, world!", foo=123)
Here we pass title
and foo
into the template. Those names are
automatically available to template as it executes. I've also passed a foo
argument to show that there's no limit on the number or types of arguments
that can be passed to templates.
Need to sleep now. Thanks for stopping by...