quick search:
 

A better way to image tags

Submitted by: jccooper
Last Edited: 2004-10-14

Category: Python(Script)

Average rating is: 0.0 out of 5 (0 ratings)

Description:
Image objects store all kinds of useful metadata, like title, height, width, and location. Using the information Zope provides for you (for free!) can make managing your images much easier: no searching through your pages to change the height or width when the actual image changes.

This is a newer version of the technique in http://www.zopelabs.com/cookbook/1031080450


Source (Text):
return '<img src="%s" width="%s" height="%s" alt="%s" />' % (
          context.absolute_url(), context.width, context.height, context.title)

Explanation:
This code is put into a Python Script (say 'jccimage') in or above
wherever you want to use it (usually the root).
To get a dynamic image tag all you need do is call this script
in context of an image:

ZPT: <span tal:replace="structure container/jcc_logo/jccimage">Some image</span>
DTML: <dtml-var expr="jcc_logo.jccimage()">
PyScript: container.jcc_logo.jccimage()

Note that this is a presentation Python script, and not a logic one.


Comments:

it can be even easier by sbrauer - 2004-10-14
By default when a Zope Image object is represented as a string it formats itself as a nice img tag (complete with width, height, and alt attributes).
So for example in a page template (assuming you have a folder named "pics" with an image named "some_image"), something like this:
<span tal:replace="structure here/pics/some_image">an image</span>

would output something like this:
<img src="http://localhost.localdomain:8080/test/pics/some_image" alt="Image Title" title="Image Title" height="384" width="512" border="0" />

In DTML, this would give the same result:
<dtml-var expr="pics.some_image"> 
 
Re: it can be even easier by jccooper - 2004-10-14
Agreed. This technique is useful when you want an image to do something that the object-generated tag won't do, like wrap itself in a table or have other attributes.
 
Re: Re: it can be even easier by Nummer5 - 2004-10-14
For things the image doesn't do by default, the tag method can be 
called explicitely. There is even a default behaviour for title 
and alt attributes; if the image has an alt property, it is used.

By the way, the alt attribute is for textmode browsing (and for 
unavailable images), and the title attribute is for tooltips. 
I'd suggest not to confuse them.