Just when I thought I understood how to use _['var' + x] as an equivalent of eval() within DTML, this one trips me up : /index_html /Images /imgDogs /imgCats why does this work : <dtml-with Images> <dtml-var "_['img' + x]"> </dtml-with> where x = 'Dogs' But this does not : <dtml-var "_['Images.img' + x]"> I've checked and this certainly would have worked : <dtml-var "Images.imgDog"> Thanks, chas
chas wrote:
Just when I thought I understood how to use _['var' + x] as an equivalent of eval() within DTML, this one trips me up :
[snip]
But this does not : <dtml-var "_['Images.img' + x]">
This would find a single object which has Id ('Images.img'+x). In fact, one of the primary reasons for using _[''] notation in expressions is to allow references to Ids with periods and other characters in them which are not legal in Python identifiers. _[] is not like eval(), it's a simple namespace lookup. The DTML equivalent of eval() is <dtml-var expr="expression"> (no, I'm not kidding!) In your case, you want either <dtml-with Images><dtml-var "_['img'+x]"></dtml-with>, or my unreleased Product which allows expressions like <dtml-var "_['Images/img'+x]">.
But this does not : <dtml-var "_['Images.img' + x]">
This would find a single object which has Id ('Images.img'+x).
Aaaah, as opposed to the object with Id ('img' + x) which exists within the folder 'Images'. So, that also explains why : <dtml-with "Imports"> <dtml-var "_['Method' + 'X']"> </dtml-with> is equivalent to : <dtml-var "Imports.MethodX(_.None, _)"> But why this won't work : <dtml-var "_['Imports.Method' + 'X']"> This latter example tripped me up for 2 hours as I played with various combinations such as : - <dtml-var "_['Imports.Method' + 'X()']"> to - <dtml-var "_['Imports.Method' + 'X(_.None, _)']"> - etc
<dtml-with Images><dtml-var>"_['img'+x]"></dtml-with>
Yep, that's what I finally worked out through trial and error. Being the stubborn pig cost me the extra hours because I don't like just accepting things, I want to know why. It also seems that this is likely to trip many people up since it is not consistent with one's ability to write this : <dtml-var "Images.imgDog"> or <dtml-var "Imports.MethodName(_.None, _)"> which do NOT refer to singular items with ID's 'Images.imgDog' and 'Imports.MethodName' respectively. In fact, this doesn't look very consistent to me. And *this* is the sort of stuff that really needs to be explained in foolproof, layman terms. Anyway, thanks big time Evan, at least my earlier problems have an explanation for them - I hate getting things to work and not knowing why. chas ps. returning to my comments in reply to Picasso's mail, how would/should I have debugged this properly then ? I'm relying on the Force a bit too much at the moment.
chas wrote:
In fact, this doesn't look very consistent to me. And *this* is the sort of stuff that really needs to be explained in foolproof, layman terms.
Sad but true, and there isn't a whole lot that can be done about it except try to make canonical ways of doing these things prominent in the documentation. If we changed _[] to allow periods as object separators, we'd break the ability to have Ids with periods in them, and while I've already hacked using '/' as a separator, it's not really practical to have Python expressions use that notation.
Anyway, thanks big time Evan, at least my earlier problems have an explanation for them - I hate getting things to work and not knowing why.
Glad to help.
ps. returning to my comments in reply to Picasso's mail, how would/should I have debugged this properly then ? I'm relying on the Force a bit too much at the moment.
I rely on the Source, for a powerful ally it is. Sadly, it also takes a lot of reading and Python knowlege, and I can't really suggest a better way.
participants (2)
-
chas -
Evan Simpson