Re: [Zope] DTML Doc property from DTML Method in tag
At 01:20 PM 2/15/99 -0600, rkw@dataplex.net wrote:
this is my structure:
Folder 'HERE' has Folders within it. Assume that they have ids ID1, ID2, ID3, etc. Each of these Folders has a property 'common_name'
Thus, in HERE/ID1 can reference <!--#var common_name--> and get 'Name 1'.
In HERE, I execute a form. The checkboxes build a list ids ['ID1', 'ID2']. Processing the form, <!--#var ids--> gives me the list. <!--#in ids--><!--#var sequence-item--><!--#/in--> gives 'ID1' on the first iteration. But I want to get 'Name 1'
Indirection is bit confusing in DTML. Here are examples from a post I made a couple days ago which tries to explain the concept: Suppose that a DTML variable named Category is 'Expensive'. Then <!--#var Category--> is 'Expensive'. Then <!--#var expr="_[Category]"--> is <!--#var expr="_['Expensive']"--> which means <!--#var Expensive-->. Got it? Here's one final example in Python from DocumentTemplate import HTML t=HTML("""<!--#var expr="_['foo']"-->, <!--#var expr="_[foo]"-->""") print t(foo="bar", bar="spam") which prints "bar, spam". This is analogous to Python. foo.bar is the same things as getattr(foo,'bar') <!--#var bar--> is the same thing as <!--#var "_['bar']"--> In fact, you might think of the DTML underscore namespace mapping as similar to a Python __dict__ namespace mapping. So to get back to your question,
<!--#var ids--> gives me the list. <!--#in ids--><!--#var sequence-item--><!--#/in--> gives 'ID1' on the first iteration. But I want to get 'Name 1'
You have a list of object ids. You want to iterate through the objects and print each one's common_name, correct? Here's one way to do that: <!--#in ids--> <!--#var "_[_['sequence-item']].common_name"--> <!--#/in--> This assumes that common_name is a string attribute of each object. If common_name were a method, or a DTML method you might want to do something different, for example: <!--#in ids--> <!--#with "_[_['sequence-item']]"--> <!--#var common_name--> <!--#/with--> <!--#/in--> This would allow normal DTML var rendering behavior. So why the two levels of indirection? Let's see. Assume ids is ['ID1','ID2']. The first time through the loop <!--#var sequence-item--> == <!--#var "_['sequence-item']"--> == 'ID1' What you are looking for is the object named ID1, not the string 'ID1'. So you have to look in the DTML namespace for the object. <!--#var "_['ID1']"--> == <!--#var "_[_['sequence-item']]"--> It's a little ugly but it works. Make sense? -Amos
At 01:20 PM 2/15/99 -0600, rkw@dataplex.net wrote:
this is my structure:
Folder 'HERE' has Folders within it. Assume that they have ids ID1, ID2, ID3, etc. Each of these Folders has a property 'common_name'
Thus, in HERE/ID1 can reference <!--#var common_name--> and get 'Name 1'.
I have a couple of simple DTML scripts that exercise a lot of the constructs discussed here. One of them manages a multiple choice test collects the results and builds a score list. If anyone is interested I can send them to him. Pavlos
On Mon, Feb 15, 1999 at 01:30:51PM -0800, Amos Latteier wrote: ,----- [lots deleted] | You have a list of object ids. You want to iterate through the objects and | print each one's common_name, correct? | | Here's one way to do that: | | <!--#in ids--> | <!--#var "_[_['sequence-item']].common_name"--> | <!--#/in--> Hmm. This doesn't seem to work for me, and it is (I think) essentially the same situation. I have a Folder with a lines property that contains ids of DTML Documents. Within my #in tags: <!--#var sequence-item--> will display "test" (which is the ID of my document) <!--#var test.title--> will display the title of that document <!--#var "_[_['sequence-item']].title"--> gives me an attribute error. I just tried this again, but using a Folder instead of a DTML Document. This works for the Folder, but not for the DTML Doc. Is this a bug in the DTML Document, or am I missing something? Actually, I'd feel better if it's a bug, because I thought I was really beginning to grok this stuff until I couldn't make that work :) Kevin `----- -- Kevin Dangoor kid@ans.net / 734-214-7349
At 06:28 PM 2/15/99 -0500, Kevin Dangoor wrote:
On Mon, Feb 15, 1999 at 01:30:51PM -0800, Amos Latteier wrote: | <!--#in ids--> | <!--#var "_[_['sequence-item']].common_name"--> | <!--#/in-->
Hmm. This doesn't seem to work for me, and it is (I think) essentially the same situation.
I have a Folder with a lines property that contains ids of DTML Documents.
Within my #in tags: <!--#var sequence-item--> will display "test" (which is the ID of my document) <!--#var test.title--> will display the title of that document <!--#var "_[_['sequence-item']].title"--> gives me an attribute error.
I just tried this again, but using a Folder instead of a DTML Document. This works for the Folder, but not for the DTML Doc. Is this a bug in the DTML Document, or am I missing something?
My bad! This is what I meant to say: <!--#var "_.getitem(_.getitem('sequence-item')).title"--> I did not understand the difference between _[] and _.getitem. The DTML User's Guide explains that _[] automatically calls objects, while _.getitem does not by default. So the reason you were getting an attribute error is that Zope was calling the Documents and then looking for a 'title' attribute in the resulting string. time-for-me-to-reread-the-dtml-users-guide-ly yrs, -Amos
participants (3)
-
Amos Latteier -
Kevin Dangoor -
Pavlos Christoforou