Patch: Add versatile "tag" method to Image
The Image class has a str() method that outputs an <IMG ...> tag with an absolute URL, and with the height, width, and alternate text set for you. This is a very efficient way to embed images in your documents, because the page renders better when the height and width of the image are given and you don't have to hard-code them into your document, and the absolute URL uses the web browser's cache efficiently. However, I found this method constraining in that I could not add attributes such as "BORDER=0" to the tag. This patch adds a more versatile tag() method to Image. Arguments to the tag() method can be any of the usual IMG attributes. Defaults are provided for height and width (again, set from the image file) and For example: <!--#var "MyImage.tag(border=0, align='CENTER', hspace=2, vspace=2)"--> ... will result in the image being rendered with the given attributes. Can we get something like this included in the main Zope thread? Thanks Bruce Perens --- Zope.old/lib/python/OFS/Image.py Mon May 24 20:49:12 1999 +++ Zope-2.0.0a3-src/lib/python/OFS/Image.py Tue Jul 13 18:52:51 1999 @@ -133,7 +133,7 @@ ('View management screens', ('manage','manage_main', 'manage_uploadForm',)), ('Change Images and Files', ('manage_edit','manage_upload','PUT')), - ('View', ('index_html','view_image_or_file','getSize','getContentType', + ('View', ('index_html','tag','view_image_or_file','getSize','getContentType', '')), ('FTP access', ('manage_FTPstat','manage_FTPget','manage_FTPlist')), ('Delete objects', ('DELETE',)), @@ -363,7 +363,35 @@ self.absolute_url(), width, height, self.title_or_id() ) + def tag(self, height=None, width=None, alt=None, **args): + """ + Generate an HTML IMG tag for this image, with customization. + Arguments to self.tag() can be any valid attributes of an IMG tag. + 'src' will always be an absolute pathname, to prevent redundant + downloading of images. Defaults are applied intelligently for + 'height', 'width', and 'alt'. + """ + if not alt: + alt=self.title_or_id() + + string='<img src="%s" alt="%s"' % (self.absolute_url(), alt) + + if not height: + height = self.height + if height: + string = "%s height=%s" % (string, height) + + if not width: + width = self.width + if width: + string = "%s width=%s" % (string, width) + + for key in args.keys(): + value = args.get(key) + string = "%s %s=%s" % (string, key, value) + return string + '>' + def cookId(id, title, file): if not id and hasattr(file,'filename'):
At 21:15 13/07/99 , bruce@perens.com wrote:
However, I found this method constraining in that I could not add attributes such as "BORDER=0" to the tag. This patch adds a more versatile tag() method to Image. Arguments to the tag() method can be any of the usual IMG attributes. Defaults are provided for height and width (again, set from the image file) and For example:
Wonderful! A few comments: - I cannot remove the alt, width or height attrinutes. Yes, this is sometimes necessary. - Attributes are not quoted, except src and alt. This is needed with width and height when using percentages. Quotes are generally a good idea, even if the HTML spec says they are optional. Attached patch (against the latest CVS), solves these points. I changed addition of the alt attribute to conditional, and added quoting for all tags. I also changed the way alt, width and height are defaulted. The code now checks wether they are equal to None, so you can do things like: imagename.tag(alt='', width='', height='') to get an IMG tag without any alt, width or height attributes. -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | Tel: +31-35-7502100 Fax: +31-35-7502111 | mailto:mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ------------------------------------------
participants (2)
-
bruce@perens.com -
Martijn Pieters