[ZPT] How Do I Use ZPT Output In Python Code?

Shane McChesney shane@nooro.com
Sun, 11 Aug 2002 22:42:53 -0400


I'm building my first-ever Zope app, using ZPTs and a 
custom Python product with a class called Question.py. This 
is also my first serious Python-based project, so please 
bear with any stupid questions.

I've been building online forms / surveys / tests for some 
time now in ASP3, and have wanted out of that platform for 
some time. 

The Zope/Python combo is already letting me improve my 
results, and I'm sure I've only seen the tip of the iceberg 
so far. I'm hitting a lot of first-timer stumbling blocks, 
but each one teaches me something important, and I'm 
enjoying myself in a way I haven't ever with ASP.

The latest hurdle is (I hope) simple enough, although it 
may shine light on spots where I'm entirely ignorant, so 
feel free to point that out.

I assemble a Survey from a bunch of Questions. Each 
Question has an XHTML layout based on a small-T template: 
"vertical radio buttons", "text", "2 by 4 grid of 
checkboxes", whatever. I use ZPTs for each of these base 
"question templates," and I want to use the XHTML generated 
by the selected question template in a string property of 
the Question instance. 

That is, I want to then be able to manually edit the 
generated presentation logic through the ZMI, and then have 
the contents of that property used later on in other ZPTs 
that actually make up the survey.

I want to do it this way because my base question templates 
cannot possibly anticipate every "question type" 
(arrangement of form controls) that a client will dream up. 
(I say "client," not "user" because I'm the intended end 
user.) 

So I'll generate starting code from a ZPT in the ZMI, edit 
it as an object property in the ZMI, and then it will be 
incorporated into whatever page that question ends up on, 
via a final ZPT.

Not sure how clear that is, but post any follow-up 
questions and I'll respond as quickly as possible. Sorry if 
this is long: I figure the more details I give, the more I 
can learn. If you have any feedback on the approach, I'd 
love to hear it, but the immediate problem I've hit is 
this:

I have built a management page that lets me click on a 
button to fire the "setQuestionUI" method, which should 
apply the question template ZPT to the Question instance, 
then store the results in the "QuestionUI" property of the 
instance. It runs without erroring out (at least the 
versions below do), but I don't get the expected result. I 
want the XHTML results in that property, not the stuff 
you'll see below.

Here are the two approaches I have so far that don't blow 
up in my face, but don't give the desired results, either.

--------------------------------------------------
This approach...

=09def setQuestionUI(self, REQUEST =3D None):
=09=09"Generate the appropriate XHTML representation for this 
question."
=09=09thePTFile =3D PageTemplateFile('www/viewQuestion', 
globals(), __name__ =3D 'thePTFile')
=09=09self.QuestionUI =3D thePTFile 

=09=09if REQUEST is not None:
=09=09=09return self.manage_main(self, REQUEST)

..populates the QuestionUI property with...

<PageTemplateFile instance at 02197AD0>
--------------------------------------------------
While this approach...

=09def setQuestionUI(self, REQUEST =3D None):
=09=09"Generate the appropriate XHTML representation for this 
question."
=09=09thePTFile =3D PageTemplateFile('www/viewQuestion', 
globals(), __name__ =3D 'thePTFile').document_src()
=09=09self.QuestionUI =3D thePTFile

=09=09if REQUEST is not None:
=09=09=09return self.manage_main(self, REQUEST)

..populates the QuestionUI property with...

<!-- Page Template Diagnostics
 Macro expansion failed
 exceptions.TypeError: object of type 'string' is not 
callable
-->
--------------------------------------------------

I'm not sure what to try next. I've been scanning the list 
archives for "document_src", but haven't found an answer 
that helps so far, so I thought I'd throw this out there.

I know I'm missing something, hopefully something obvious.

Thanks for your time,

Shane McChesney
http://www.skippingdot.net

Most recent post on Zope:
http://www.skippingdot.net/2002/07/04