A Brief Introduction to Kid Templates
| Status: | Official |
|---|
Kid templates can be any XML document with namespaced attributes that tells Kid how to process the template. In practice, your templates will be XHTML documents that will be processed and transformed into valid HTML documents.
This example (straight from Kid's documentation) shows what Kid is like:
<?python
title = "A Kid Test Document"
fruits = ["apple", "orange", "kiwi", "M&M"]
from platform import system
?>
<html xmlns:py="http://purl.org/kid/ns#">
<head>
<title py:content="title">This is replaced.</title>
</head>
<body>
<p>These are some of my favorite fruits:</p>
<ul>
<li py:for="fruit in fruits">
I like ${fruit}s
</li>
</ul>
<p py:if="system() == 'Linux'">
Good for you!
</p>
</body>
</html>Important points about Kid templates:
- Don't forget to define the py XML namespace (as is done in the <html> tag above). That's key to having your template understood as valid XML.
- ${} lets you easily substitute in a Python expression anywhere in your document.
- $foo lets you substitute in the variable foo, but it's not as safe as using the {} because it can be harder to detect where you substitution is supposed to end
- This is XHTML, so you need to close all of your tags. For example, in HTML you'd write <br> to put a line break. In XHTML, you need to write <br/>. This will get converted properly to HTML on the way out.
- Since the template needs to be valid XML, if you have JavaScript with < and >, you should enclose the script in a <![CDATA[ javascript here ]]> section.
- There are some reserved words in Kid templates: write, serializer, serialize, generate, initialize, pull, content, transform. If you use those words as template variable names, you'll likely get an error.
One of the great things about Kid is that everything you know about Python applies here. Kid templates get compiled to Python code this makes it easier to predict how things will behave. For example, that py:for="fruit in fruits" behaves just like for fruit in fruits: in Python.
The variables that you defined in the dictionary returned by your controller are all available for your use. In any of the py attributes, just use the variable from the dictionary as you would a local variable in Python. In the py:for="fruit in fruits" example, fruits would be some kind of iterable object passed in via the dictionary.
When variables are dropped in to your template, Kid will automatically escape them. You don't even need to think about running into problems with values that contain <, for example. The time when you do need to care is if you actually have XHTML itself to drop in place. If you do, wrap your substitution value in XML(). For example, let's say we had an XHTML fragment called header. You could just say ${XML(header)}, and the header would be dropped in without being escaped.
Rather than reproduce it here in full, a quick read that is well worth it is the reference to Kid's attribute language.
Previous: Using Your Model : Next: Template Variables You Get for Free
| localhost | Unfortunately, kid templates do not preserve the placement of static text in the template. This was a showstopper for me, as it inserted spaces between image tags in my html output, causing any browser to put spaces between the images. Unacceptable. |
2006-10-11 08:23:43 X | ||
| localhost | Same comment as above but my problem was whitespace being added inside textareas. |
2006-11-03 14:13:28 X | ||
| localhost | have these issues been addressed anyone?!? |
2007-03-17 05:42:21 X | ||
| localhost | <p py:if="'${dr2.id}'=='${dr1.kaartsoort.id}'"> <b>OK</b> </p> why can't i get this working? when the two in the if are equal, it wil not print OK |
2008-01-04 05:02:01 X | ||
| ChristopherArndt | Why are you putting both values into single quotes? I guess, the 'id' attributes are integers. Have you tried comparing them directly? |
2008-01-04 07:49:58 | ||
| localhost | <p py:if="dr2.id==dr1.kaartsoort.id"> this worked: no quotes and no ${} thanks |
2008-01-09 06:25:23 X | ||