Create a header using TAL and a Z SQL Method in Zope

From Julian Yap

Access the first record before asking for its attributes. Also, path expressions don't play nicely with integer indexes, so you need to use Python:

<tr tal:define="customer container/getCustomerLicences" tal:condition="customer">
    <td><b>Customer: </b><span tal:content="python:customer[0]['full_name']"></span></td>
</tr>

The above condition checks that 'customer' is not empty.

Use 'tal:condition repeat/licenses/start' to test that you are in the first pass through the loop.

Original problem

The following code where getCustomerLicenses is my Z SQL Method and it prints out the customer's full name with details:

<tr tal:repeat="licenses container/getCustomerLicences">
    <td><span tal:replace="licenses/full_name">full_name</span></td>
    <td><span tal:replace="licenses/serial_number">serial_number</span></td>
<!-- code continues with more details -->
</tr>

In it's simplest form, how do I retrieve just the full_name of the first record using TAL. I think I use tal:replace but I'm not sure.

I was along the lines of something like this:

<tr tal:define="x container/getCustomerLicences">
    <td tal:replace="x/full_name[0]></td>
</tr>

But then I just get a TypeError.

Wrap it in a Python script example

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.

The keyword arguments, if any, that were passed to the template. When a template is rendered from the web, no options are present. Options are only available when a template is called from Python or by similarly complex means. For example, when the template t is called by the Python expression t(foo=1), the path options/foo equals 1.

Links