Views are the components that process the results of an Action and then format and output those results to the user's browser. They are represented by the "pywork.view.ViewHandler" class (which you can find in "pywork/view.py"). The 3 view techonologies included in the current PyWork ditribution are:
Also remember that you can customize the content type header by specifying it in pywork-map.xml, by default PyWork will set it to "text/html". At the time of writing this where the more useful and interesting technologies that we found. Each one has a different behaviour and advantages. Lets see in detail each one of them.
from pywork.action import Action class MyAction(Action): def __init__(self): super(MyAction,self).__init__() self.__petList = ('Reptiles','Dogs','Cats','Birds') def __getPets(self): return self.__petList pets = property(__getPets) def execute(self): return "pets_show"
The piece of "pywork-map.xml" that defines this action and its view:
<pywork-map> <action name="/mysimpleapp/pets.action" module="simpleapp.webactions" class="MyAction"> <view name="pets_show" file="pets_show.zpt.html" view-handler="ZPT"/> </action> </pywork-map>
And the file "pets_show.zpt.html":
<html> <head><title>Simple App</title></head> <body> We have this categories of pets: <span tal:repeat="cat here/pets"> <span tal:replace="cat">sample category</span><br/> </span> </body> </html>
This example will output the following html:
<html> <head><title>Simple App</title></head> <body> We have this categories of pets: <span> Reptiles<br/> Dogs<br/> Cats<br/> Birds<br/> </span> </body> </html>
In this way you can access your action results and process your output, using ZPT powerful template syntax, to generate HTML and XML. You may also access (though is not recommended) python directly by the use of "python:" special TALES syntax. The variable "here" will also be available from there, pointing of course to the action that has executed, as well as all the builtin python functions.
Keeping this rules in mind you will always know what should be exposed to your XSL's. Again we recommend to expose readable properties as the convencional way to access your action results, and do not leave "public" attributes that you dont intend to access. Also for your convenience we have written this module in C and Python, so if you are able to build the C module (compiles on every platform Python does), you will get the highest performance, but don't worry the python version is also very fast. Let's continue to examin how the XML is generated (see pywork/util.py for more detailed information). The XML contains as the root node, the element "result", everything is put under this element. Furthermore, structures like list, sequence and dictionaries are introspected and output. Let's see a fairly complex example:
from pywork.action import Action class Pet(object): def __init__(self): self.name = "Charly" self.type = "Dog" class TestParent(object): def __init__(self): self.grandfather = "Armando" self.friends = ( "MARTIN", "JUAN", "LUCAS" ) class Test(TestParent): def __init__(self): super(Test,self).__init__() self.name = "Julian" self.surname = "Ciccale" self.__contact = { "PHONE":"1234567", "ADDRESS":('Re umberto','118') } def __getContact(self): return self.__contact contact = property(__getContact)
To get the XML version of this we can do this
>>> from pywork.util import action2xml >>> action2xml(Test()) <result><contact><PHONE>1234567</PHONE><ADDRESS><item>Re umberto</item><item>118</item></ADDRESS></contact><friends><item>MARTIN</item><item>JUAN</item><item>LUCAS</item></friends><grandfather>Armando</grandfather><name>Julian</name><surname>Ciccale</surname></result> >>>
Lets examine in more detail the XML
<result> <contact> <PHONE>1234567</PHONE> <ADDRESS> <item>Re umberto</item> <item>118</item> </ADDRESS> </contact> <friends> <item>MARTIN</item> <item>JUAN</item> <item>LUCAS</item> </friends> <grandfather>Armando</grandfather> <name>Julian</name> <surname>Ciccale</surname> </result>
In the above example we find differente elemnts, lets examine each one in detail:
Attention, for numbers you the python default str() method will be called so if you care about the format , please format it yourself and save it as a string. Please note that none of the "pywork.action.Action" attributes are output, this is done in purpose to avoid confussion and bad use of the tool. As stated int the above example, Sequences (List or Tuples) elements are inside an "item" XML element. And Dictionaries elements are inside an XML element with the name of the key.
Let's see how you would write an XSL to process the same information of the pets example showed in the ZPT section:
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/TR/xhtml1/strict"> <head><title>Simple App</title></head> <body> We have this categories of pets: <span> <xsl:for-each select="result/pets/item"> <xsl:value-of select="text()"/><br/> </xsl:for-each> </span> </body> </html>
This will output the same HTML as the ZPT example.
See About this document... for information on suggesting changes.