[ZPT] Content/Presentation Separation

Harry Wilkinson harryw@nipltd.com
Tue, 23 Apr 2002 17:40:40 +0100


On Tuesday 23 April 2002 5:23 pm, Brian Lalor wrote:

> On Tue, 23 Apr 2002, Harry Wilkinson wrote:
> > Defining how you want it presented (like in your print statement there)
> > is what ZPTs are for.  So, yes, use ZPTs for that.
> >
> > Say you have a script that returns the value of resRebatePrice, called
> > getResRebatePrice.... to present this information in a ZPT-ish way, you
> > could write this in the ZPT:
> >
> > <div tal:replace="string:Quantity Special: Each box is only
> > ${here/getResRebatePrice} a box!"> your formatted string will replace
> > this text </div>
>
> Let's say I want to do a product presentation page of some type; the
> product id is passed as a param on the URL (via GET).  How would I
> instantiate a product object and then show individual properties of that
> object?  <foo tal:define /> or somesuch?

Well, if you want to instantiate an object from a class, using the product ID 
in the constructor, you need to first acquire your class:

<tal:gettheclass define="myClass here/path/to/the/class/theClass">

... code in here using the class ...

</tal:gettheclass>

Note that the tag name is unimportant, just named for convenience.  Tags in 
the TAL namespace don't get rendered, which is why I've put this in the TAL 
namespace.  We're not rendering, just defining.

Now if you pass the id into the page on the querystring of the request (ie 
URL?id=whatever) then the 'id' variable will be in the 'request' structure.  
You can make your class using the ID in the constructor like this:

<tal:gettheclass define="myClass here/path/to/the/class/theClass">

  <tal:instantiate define="productobject python:myClass(id=request['id'])">

   ... code here to display your product object

  </tal:instantiate>

</tal:gettheclass>


Hopefully that's clear enough.  Note that "request/id" in a TAL path 
expression is the same as "request['id']" in a Python expression.


Harry