CherryPy - Presentation Layer



The Presentation Layer ensures that the communication passing through it targets the intended recipients. CherryPy maintains the working of presentation layer by various template engines.

A template engine takes the input of the page with the help of business logic and then processes it to the final page which targets only the intended audience.

Kid — The Template Engine

Kid is a simple template engine which includes the name of the template to be processed (which is mandatory) and input of the data to be passed when the template is rendered.

On creation of the template for the first time, Kid creates a Python module which can be served as a cached version of the template.

The kid.Template function returns an instance of the template class which can be used to render the output content.

The template class provides the following set of commands −

S.No Command & Description
1.

serialize

It returns the output content as a string.

2.

generate

It returns the output content as an iterator.

3.

write

It dumps the output content into a file object.

The parameters used by these commands are as follows −

S.No Command & Description
1.

encoding

It informs how to encode the output content

2.

fragment

It is a Boolean value which tells to XML prolog or Doctype

3.

output

This type of serialization is used to render the content

Example

Let us take an example to understand how kid works −

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:py = "http://purl.org/kid/ns#">
   <head>
      <title>${title}</title>
      <link rel = "stylesheet" href = "style.css" />
   </head>
	
   <body> 
      <p>${message}</p>
   </body>
</html>

The next step after saving the file is to process the template via the Kid engine.

import kid

params = {'title': 'Hello world!!', 'message': 'CherryPy.'}
t = kid.Template('helloworld.kid', **params)
print t.serialize(output='html')

Kid's Attributes

The following are the attributes of Kid −

XML-Based Templating Language

It is an XML-based language. A Kid template must be a well-formed XML document with proper naming conventions.

Kid implements attributes within the XML elements to update the underlying engine on the action to be followed for reaching the element. To avoid overlapping with other existing attributes within the XML document, Kid has introduced its own namespace.

<p py:if = "...">...</p>

Variable Substitution

Kid comes with a variable substitution scheme and a simple approach — ${variable-name}.

The variables can either be used in attributes of elements or as the text content of an element. Kid will evaluate the variable each and every time the execution takes place.

If the user needs the output of a literal string as ${something}, it can be escaped using the variable substitution by doubling the dollar sign.

Conditional Statement

For toggling different cases in the template, the following syntax is used −

<tag py:if = "expression">...</tag>

Here, tag is the name of the element, for instance DIV or SPAN.

The expression is a Python expression. If as a Boolean it evaluates to True, the element will be included in the output content or else it will not be a part of the output content.

Looping Mechanism

For looping an element in Kid, the following syntax is used −

<tag py:for = "expression">...</tag>

Here, tag is the name of the element. The expression is a Python expression, for example for value in [...].

Example

The following code shows how the looping mechanism works −

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
      <title>${title}</title>
      <link rel = "stylesheet" href = "style.css" />
   </head>
	
   <body>
      <table>
         <caption>A few songs</caption>
         <tr>
            <th>Artist</th>
            <th>Album</th>
            <th>Title</th>
         </tr>
			
         <tr py:for = "info in infos">
            <td>${info['artist']}</td>
            <td>${info['album']}</td>
            <td>${info['song']}</td>
         </tr>
      </table>
   </body>
</html>

import kid

params = discography.retrieve_songs()
t = kid.Template('songs.kid', **params)
print t.serialize(output='html')

The output for the above code with the looping mechanism is as follows −

Looping Output
Advertisements