5. Sample Application

Here we present a simple example. We present the python source code and the several code for the different views.

The configuration files:


    	<global name="reload" value="0"/>
    	<global name="MY_TEMPLATES" value="/myapp/web/templates"/>


	<action name="/hello.action" module="dummy" class="HelloAction">
		<view name="success_zpt" file="${MY_TEMPLATES}/hello_zpt.html" view-handler="ZPT"/>
		<view name="success_xsl" file="${MY_TEMPLATES}/hello.xsl" view-handler="XSLT"/>
		<view name="error" file="${MY_TEMPLATES}/error_zpt.html" view-handler="ZPT"/>

The python module:

from pywork.action import Action

class HelloAction(Action):
	def __init__(self):
		self.__view = None
		self.__name = None
		self.__message = None
		self.__submitted = None

	def __getMessage(self):
		return self.__message
	def __setName(self,s):
		self.__name = s

	def __setView(self,s):
		if s in ( "ZPT", "XSL" ):
			self.__view = s

	message = property(__getMessage)
	name = property(None,__setMessage)
	view = property(None,__setView)
	submitted = property(None,__setSubmitted)

	def execute(self):
		if not self.__submitted or not self.__name or not self.__view:
			self.__message = "You must fill the form"
			return "error"

		self.__message = "Hello %s !" % self.__name

		if self.__view == "ZPT": return "success_zpt"
		if self.__view == "XSL": return "success_xsl"

Lets take a look at the code. The import statement imports the class "Action" from the "pywork.action" module. Then the class declaration extends from "Action", and declares 4 attributes. Also, 3 properties are declared "message" , read-only; "name", "view" and "submitted" write-only. If you take a look at "__setView", you will find that does some parameter chechking, i.e., if the view is not "XSL" or "ZPT" does not set it. The main code is found in "execute". It first checks for the correct parameters, if something fails it sets a message an returns "error". Else, it sets a "Hello Name" message and returns the proper view.

The input form: (in plain HTML for the sake of simplicity)

	<head><title>PyWork ZPT View</title></head>
	<p>Input data and hit submit</p>
	<form action="hello.action">
	Name: <input type='text' name='name/><br/>
	View: <select name='view'>
			<option value=''>Select</option>
			<option value='ZPT'>Zope Page Templates</option>
			<option value='XSL'>XSL Transformation</option>
	<input name='submitted' value='Submit'/>

The views:


	<head><title>PyWork ZPT View</title></head>
	<p>The message is: <span tal:replace="here/message">example text</span></p>


<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
	<head><title>PyWork XSLT View</title></head>
		<p>The message is: <span> <xsl:value-of select="result/message"/> </span></p>

When you chose a view, e.g.: XSL, enter a name, e.g.: "John Doe" and hit submit you will get this html:

	<head><title>PyWork XSLT View</title></head>
		<p>The message is: <span> Hello John Doe ! </span></p>

That's pretty much it. We will not explain here ZPT or XSL you will found doc links in the view section.

See About this document... for information on suggesting changes.