KidKit User's Guide

KidKit version 1.1.1

Created by Winston Wolff, January 2005.
Improved version by Christoph Zwerschke, October 2005. Last changes by Christoph Zwerschke, March 2007.

Synopsis

KidKit is a Webware plug-in which makes Webware understand "Kid" templates. By using this kit you can write Kid template files, and Webware will compile and render them automatically when they are addressed via Webware's application server. This is very similar to the concept of Python Server Pages (PSP), but Kid provides some unique features making it an amazing modern alternative to PSP.

Requirements

Contrary to PSP, Kid itself is not included in Webware. You need to install the Kid package separately.

You can download Kid from www.kid-templating.org/.

The current KidKit has been tested with Kid versions from 0.6 up to 0.9.6. Other versions may work as well.

Feedback

You can e-mail webware-discuss@lists.sourceforge.net to give feedback, discuss features and get help using KidKit. For Kid specific questions, use kid-template-discuss@lists.sourceforge.net instead.

Differences between Kid and PHP

The main differences between Kid and PHP are the following:

Configuring KidKit

There are some configuration parameters in Application.config which control the behavior of KidKit.

Writing Kid templates

In the simplest case, you can just take a Kid template file with the extension .kid and place it into a directory that is a valid Webware context. For instance, you will find the following file with the name Time2.kid in the KidKit/Examples context:

<?xml version='1.0' encoding='utf-8'?>
<?python
import time
title = "A Kid Template"
?>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:py="http://purl.org/kid/ns#">
  <head>
  <title py:content="title">
    This is replaced with the value of the title variable.
  </title>
  </head>
  <body style="color:black;background-color:white">
    <div style="font-family:sans-serif;text-align:center">
      <h2>
        Time Example 2
      </h2>
      <p>
        <i>
          This page is a stand-alone page.
        </i>
      </p>
      <p>
        The current time is ${time.strftime('%C %c')}.
      </p>
    </div>
  </body>
</html>

You can address this template as any other page in Webware; you don't need to type in the .kid extension in the URL.

KidKit automatically compiles the template to a Python module and creates a servlet class that will write the serialized template back as a response. As base servlet class WebKit.Page will be used, and the output will be written by replacing the method respond. You can configure this response "hook" by setting a variable named hook in the Kid template on the global level, i.e. before the root element is opened. For instance, if you want to have the Kid template replace the method writeContent of the servlet class KidExamplePage, you would add the following lines to the top of your Kid template:

<?xml version='1.0' encoding='utf-8'?>
<?python
from KidKit.Examples.KidExamplePage import KidExamplePage
hook = KidExamplePage.writeContent
?>
<root>...</root>

Since the html tag has already been written when writeContent is called, you must use some other tag for the root element or add py:strip="" to the tag so that it will be stripped away.

Note that Python code that has been placed at the global level of the template will be executed only once when the template is loaded for the first time. The servlet factory of KidKit will then create an appropriate servlet class that will be cached in memory by Webware from then on. Every time the servlet is instantiated, the "hook" method would serialize the template and write the result to the response.

In order to get access to the servlet from code that has been placed at the local level of the Kid template, you can access the servlet variable which gives you full access to the servlet instance. From here, you can get all other information associated with the servlet. For instance, you can get the form fields as servlet.request().fields().

Here is a simple example that reads the input from a form input field and writes it back on the screen as a greeting:

<html xmlns:py="http://purl.org/kid/ns#">
<head><title>Form Example</title></head>
<body>
<?python fields = servlet.request().fields()
name = fields.get('name') or 'stranger' ?>
<h1>Kid Form Example</h1>
<p>Hello <strong py:content="name" />, how are you?</p>
<form>
Enter your name here: <input type="text" name="name" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

Please note that though the template is written as strict XML, the output will be serialized as HTML, because it has been configured as the default output method. So the output of the servlet will look like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD><META CONTENT="text/html; charset=utf-8" HTTP-EQUIV="Content-Type">
<TITLE>Form Example</TITLE></HEAD>
<BODY>
<H1>Kid Form Example</H1>
<P>Hello <STRONG>stranger</STRONG>, how are you?</P>
<FORM>
Enter your name here: <INPUT TYPE="text" NAME="name">
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>

However, you can change the default output method with the KidOutputMethod parameter mentioned above, or you can change the output method for an individual template by setting the variable output at the global level. For instance, you could add the following line as first line (before the html tag) to the Kid template above:

<?python output='xhtml-strict' ?>

In this case, the template would be serialized like that:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Form Example</title></head>
<body>
<h1>Kid Form Example</h1>
<p>Hello <strong>stranger</strong>, how are you?</p>
<form>
Enter your name here: <input type="text" name="name" />
<input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

More examples

You will find some more examples in the KidKit/Examples context.

Credits