[Zope] Re: Output only the first or a particular record using TAL

Julian Yap julian_yap at yahoo.com
Wed Sep 28 06:07:00 EDT 2005


--- Tino Wildenhain <tino at wildenhain.de> wrote:
> Am Mittwoch, den 28.09.2005, 16:32 +1000 schrieb Julian Yap:
> > --- Chris Withers <chris at simplistix.co.uk> wrote:
> ...
> > > Now, if you really can't stop your sql returning more than
> one
> > > row 
> > > (LIMIT 1 anyone?) then how about:
> > > 
> > > <tr tal:define="customers container/getCustomerLicences;
> > >                  customer  python:customers and
> customers[0]"
> > >      tal:condition="customer">
> > >   <td>
> > >    Customer:
> > >    <span tal:content="customer/full_name"></span>
> > >   </td>
> > > </tr>
> > 
> > To have my SQL returning only just 1 row would require
> another Z
> > SQL Method, in which case it may as well be something like
> > getCustomer.
> > 
> > I'm still getting my head around TAL and how it works so I'm
> not
> > sure how the "python:customers and customers[0]" code works.
> > 
> > Ideally, I want to use just the one Z SQL method (done) but
> I
> > guess it would be better coding to just reference the Z SQL
> > method once.  Currently the page template makes two calls to
> the
> > Z SQL method.  Once for the customer header and another to
> > retrieve the customer license details.  Can you suggest how
> this
> > would be done?
> 
> You could just wrap it in a simple python script:
> 
> results=context.yourZSQLMethod()
> 
> h=results[:1] # see python slice
> b=results[1:]
> 
> return context.yourPageTemplate(header=h,body=b)
> 
> And in your ZPT use tal:repeat="chead options/header"
> and options/body
> 
> But maybe you can rething and rephrase your problem :-)

Thanks Tino.

Your example of using the Python script really opens my eyes to
how everything fits together (I know Python).

I'm not sure about the TAL statement you used (so what else is
new).  ... OK, I just got back from reading about the "options"
keyword in Page Templates... Very useful.

Thanks for the usage Python tip.  I think that will get me going
for a while.

Another query I had was on the practice of using control flow
(ie. if, for, etc.. statements).  I guess the best way is to
keep your control flow in Python scripts?  Is this the common practise?


More information about the Zope mailing list