checking for existing/nonexisting properties and objects?
Consider the following dtml code fragment: <dtml-in "objectValues(['Image'])"> <img src="<dtml-var id>"> <dtml-if "caption"> <caption><dtml-var caption></caption> </dtml-if> </dtml-in> I use it to loop over all images in a folder, displaying the image and its caption if such a property exists. This snippet fails if the image doesn't have a "caption" property. I've been working around this by adding an empty caption further up the tree but I'd like to avoid this if possible. On a related note, I have tried to write dtml to render an image only if the named object exists without success. Any help would be much appreciated, preferably cc'd to my private email as I receive the digest version of the list. Cheers, Darran. -- Darran Edmundson [Darran.Edmundson@anu.edu.au]
At 06:25 PM 9/12/99 +1000, Darran Edmundson wrote:
Consider the following dtml code fragment:
<dtml-in "objectValues(['Image'])"> <img src="<dtml-var id>"> <dtml-if "caption"> <caption><dtml-var caption></caption> </dtml-if> </dtml-in>
I use it to loop over all images in a folder, displaying the image and its caption if such a property exists. This snippet fails if the image doesn't have a "caption" property.
That's actually correct Python functionality. <dtml-if caption> is like saying : "If the variable 'caption' has a value equal to None or '' or 0" which assumes that the variable 'caption' exists. Try <dtml-if caption> (note, no quotes!). That should work. The difference is (and somebody pls correct me if I'm wrong) that this is not using a Python expression and DTML's <dtml-if var_name> does both a check for existence as well as check of the value. Bottomline : use <dtml-if caption>
I've been working around this by adding an empty caption further up the tree but I'd like to avoid this if possible.
Sidenote, if you're cataloging a lot of images, you might find it easier to create a zclass called, say, "catalogedimage" with 2 properties : 'mycaption' and 'myimage'. Then you wouldn't have to worry about existence of a property, just the value. But maybe I'm just overboard with zclasses lately so ymmv. chas
chas wrote:
Bottomline : use <dtml-if caption>
Thanks.
Sidenote, if you're cataloging a lot of images, you might find it easier to create a zclass called, say, "catalogedimage" with 2 properties : 'mycaption' and 'myimage'. Then you wouldn't have to worry about existence of a property, just the value. But maybe I'm just overboard with zclasses lately so ymmv.
I wondered about this. I've yet to use Zclasses, using only the standard objects that come with Zope. I had thought of trying to implement a photo class that had several key features: - a caption, photographer, photo_date, photo_location, and keywords property - the ability to store several image objects (differing resolutions of same image) - the ability to render at various resolutions - buttons to select current rendering resolutions. Do you see the merit in such a class? If so, any tips on implementing this beast, specifically the containment of an arbitrary number of images. Cheers, Darran. -- Darran Edmundson [Darran.Edmundson@anu.edu.au]
At 12:31 PM 9/13/99 +1000, Darran Edmundson wrote:
Sidenote, if you're cataloging a lot of images, you might find it easier to create a zclass called, say, "catalogedimage" with 2 properties : 'mycaption' and 'myimage'. Then you wouldn't have to worry about existence of a property, just the value. But maybe I'm just overboard with zclasses lately so ymmv.
I wondered about this. I've yet to use Zclasses, using only the standard objects that come with Zope.
My advice : use them now. Give it a shot. Perhaps it won't work out but it's better to learn about them now than 2 months down the line. Once I found out how easy they are and how flexible, I went back and rewrote almost all my code to use them - it's much more scalable (for my needs anyway). They could potentially save you alot of time if your project is going to grow more complex. There are a few quirks in more complex zclasses (see my other queries about adding zsql methods and the problems with adding zclasses to zclasses ... but as more and more people come across these, it becomes easier for you.) Ok, going to your example :
I had thought of trying to implement a photo class that had several key features:
- a caption, photographer, photo_date, photo_location, and keywords property - the ability to store several image objects (differing resolutions of same image) - the ability to render at various resolutions - buttons to select current rendering resolutions.
Do you see the merit in such a class?
Definitely. It would be one hell of a headache to do this any other way. The best thing about zclasses is that if you find after 2 years that you also need to add a new property (eg. cost for purchase) then you can update the class and all instances will be automatically updated. Ditto for methods (eg. adding a method for "purchase_this") The big question here though is the scalability of zodb for a photo-catalog. On the MySQL list, the general consensus is that keeping files on the filesystem is the way to go (as opposed to storing GB's of images in the DB). However, Zope saves you from having to deal with extracting the image from the DB (which is real hassle) so that's no longer an issue. Personally, I think scalability is still better keeping photos outside... you could keep the photos on several disks or servers all fed from the one zope server. I may be wrong. But even if you're just including the URL's to the images, the zclass will still be a saviour. Sidenote : one reason *to* keep the images in the Zope ODB is that you could assign privileges - good if you're charging for view.
If so, any tips on implementing this beast, specifically the containment of an arbitrary number of images.
This is something that really needs to be spelled out in black and white in the zclasses tutorial. (The tutorial is excellent for getting you up and running with the basics of zclasses but beyond that, Kevin's kmnews product *is* the best reference for learning about advanced zclass concepts.) Anyway, to create a zclass that can contain other zclass's, just make sure that the container zclass is based on the ObjectManager class. (don't be scared by the term ObjectManager - it is as simple as it sounds - it stores objects. It allows you to create a zclass that can contain other zclasses.) Hope that helps get you off the ground - email back if further problems, lord knows I had my share fair. chas
Anyway, to create a zclass that can contain other zclass's, just make sure that the container zclass is based on the ObjectManager class. (don't be scared by the term ObjectManager - it is as simple as it sounds - it stores objects. It allows you to create a zclass that can contain other zclasses.)
hi, i've tried the zclass tutorial, and everything went OK. but when trying to create a zclass within a zclass, i can't see the zclass when i want to create an instance of it. e.g. i got zclass B in zclass A. after creating an instance of A, i can't add zclass Badd did make zclass A as an object manager. sorry if this is off topic.
At 09:40 AM 9/14/99 -0400, kedai wrote:
Anyway, to create a zclass that can contain other zclass's, just make sure that the container zclass is based on the ObjectManager class. (don't be scared by the term ObjectManager - it is as simple as it sounds - it stores objects. It allows you to create a zclass that can contain other zclasses.)
hi, i've tried the zclass tutorial, and everything went OK. but when trying to create a zclass within a zclass, i can't see the zclass when i want to create an instance of it.
But there's an "add" button, right ? Click it.
e.g. i got zclass B in zclass A. after creating an instance of A, i can't add zclass Badd did make zclass A as an object manager.
Do you see the "add" button inside your instance of Zclass A ? Clicking it should create an instance of Zclass B. If you don't see any "add" buttons or any pull down lists, go to the "subobjects" tab of Zclass A and explicitly select which objects can be added to an instance of Zclass A (since it *is* based on the ObjectManager, isn't it ?) IF you see the "add" button and clicking it gives a permissions error, then you need to go to the permissions tab of zclass A and click "change". Also, go to the factory of Zclass B and click change... this has often helped me in the past and I think it's a bug.
sorry if this is off topic.
It's not off topic. Zclasses are very powerful but there are some wrinkles that need to be ironed out (and advanced docs are a bit slack). I would write a tutorial on the latter but I fear they'd be irrelevant within the next point release - the only reason that Zclasses are causing so much trouble right now is due to glitches that will hopefully be fixed soon. chas ps. congratulations - first time I've ever seen another malaysian on a python-oriented list.
participants (3)
-
chas -
Darran Edmundson -
kedai