[Zope] image objects into table via python script

J Cameron Cooper zope-l at jcameroncooper.com
Wed Nov 17 16:32:40 EST 2004


John Schinnerer wrote:

> Hello,
>
> I am using a python script to generate an HTML table structure.
> The script is called from a page template, like this:
> <span tal:replace="structure
> python:container.renderTable('CatalogQueryParameter')" />
>
> The script (renderTable) iterates over items returned from a catalog
> query which is passed one parameter.
> To put for example a date or string property in the table is easy, like
> this:
>
> ...
> for item in resultItems:
>     print("<td>")
>     print(item.creation_date.strftime('%B %Y'))
>     print("</td>")
>         ...
> return printed
>
> What I really need to put in the table, though, are Image objects that
> are part of the objects returned.
>
> How can I put the Image object into the table structure returned by the
> script, so that the Image object will render itself as usual?  That is,
> as it would in for example:
>
> <span tal:replace="structure here/art_image" />
>
> The below of course does not work (art_image is an Image object in the
> item object) - it gives a TALES attribute error on art_image:
>
> ...
> for item in BGitems:
>     print("<td>")
>     print(item.art_image)
>     print("</td>")
>         ...
> return printed
>
> ...and I am stuck on how to get the art_image Image object into the
> table returned by the script.

First, and I think you know this, recall that you're not including the
Image object, but rather an HTML 'img' tag that will display the image.

There could be any number of problems, but make sure that what you think
has an Image object in context actually does. Are you sure that you're
not being given a BLOB or a name?

To get the tag rendered from an Image, you can call the image. So put a
pair of parentheses on the end of the name. This should be the exact
same effect as the 'tag' method. (I do this by memory, so don't take it
as gospel.)

Finally, you should re-think doing HTML in a script. That's really what
templates are for. You can use scripts for creating the list of items or
doing complex transformations, but actually creating the HTML is not
usually recommended.

         --jcc



More information about the Zope mailing list