I have a method in my root folder that draws a top navigation bar by looping through the subfolders. I don't want to publish links to all the subfolders however and prevent subfolders from being listed by checking if a folder has a property called "index_me". I also don't want to make a link active if am currently in that folder. I have a few questions after battling with syntax for about 3 hours: 1.What is the simplest way to do what i've done? (see code below) 2.What is the simplest way to check if the folder being queried in objectValues is equal to the URL in the Request variable? #Here I first check to see if i am in the root folder. #If I am I just emphasize the text, other wise a create a link to the homepage. #I also just use objectValues('Folder') to make the links if I am at the homepage and #I use PARENTS[1].objectValues('Folder') to make links if I am not <dtml-with REQUEST> <dtml-if "URL0==BASE2"> <em>Afrimusik Homepage</em> <dtml-in "objectValues('Folder')"> <dtml-if "hasProperty('index_me')"> | <a href="<dtml-var absolute_url>"><dtml-var title></a> </dtml-if> </dtml-in>|] <dtml-else> <a href="<dtml-var SCRIPT_NAME>">Afrimusik Homepage</a> <dtml-in "PARENTS[1].objectValues('Folder')"> <dtml-if "hasProperty('index_me')"> #This is where I am stuck. I simply want to compare the URL in the Request variable to #absolute_url of the folder being queried. <dtml-if "URL==absolute_url"> does not work. <dtml-if "URL==absolute_url"> | <em><dtml-var title></em> <dtml-else> | <a href="<dtml-var absolute_url>"><dtml-var title></a> </dtml-if> </dtml-if> </dtml-in>|] </dtml-if> </dtml-with> Thanks Roché Compaan
----- Original Message ----- From: Roché Compaan <roche@up-front.co.za> To: Zope <zope@zope.org> Sent: Sunday, November 07, 1999 3:26 AM Subject: [Zope] dtml-if syntax and comparing variables [big snip]
#This is where I am stuck. I simply want to compare the URL in the Request variable to #absolute_url of the folder being queried. <dtml-if "URL==absolute_url"> does not work.
<dtml-if "URL==absolute_url">
This is untested, but how about: <dtml-if "this() == PARENTS[0]">
| <em><dtml-var title></em> <dtml-else> | <a href="<dtml-var absolute_url>"><dtml-var title></a> </dtml-if> </dtml-if> </dtml-in>|]
</dtml-if> </dtml-with>
Kevin
Thanks Kevin, it works. But the solution to the problem really makes me sigh deeply. I currently have more solutions than understanding. The Zope Quick Reference quickly mentions "this" as "a handy way to talk to ourselves in document templates". I am familiar with "self" in many programming languages and "self" would have enlightened "this" issue a bit. I am not giving up because I just see too much potential in Zope. I love it but in many ways it's like a problematic child. I don't have any strategy in approaching the Zope Object Framework, except maybe that I'll have to learn Python (which I'm busy with) and start digging into the code - is this the only way? Simple issues arise: 1.What are the rules for using quotes in dtml? 2.The nitty gritty of passing, comparing and setting variables in less than 6 hours. 3.How does one properly cross the chasm from rendering variables to evaluating them in expressions. Hopefull Roché Compaan
#This is where I am stuck. I simply want to compare the URL in the Request variable to #absolute_url of the folder being queried. <dtml-if "URL==absolute_url"> does not work.
<dtml-if "URL==absolute_url">
This is untested, but how about:
<dtml-if "this() == PARENTS[0]">
| <em><dtml-var title></em> <dtml-else> | <a href="<dtml-var absolute_url>"><dtml-var title></a> </dtml-if> </dtml-if> </dtml-in>|]
</dtml-if> </dtml-with>
Kevin
----- Original Message ----- From: Roché Compaan <roche@up-front.co.za> To: Zope <zope@zope.org> Sent: Sunday, November 07, 1999 11:00 AM Subject: RE: [Zope] dtml-if syntax and comparing variables
But the solution to the problem really makes me sigh deeply. I currently have more solutions than understanding. The Zope Quick Reference quickly mentions "this" as "a handy way to talk to ourselves in document templates". I am familiar with "self" in many programming languages and "self" would have enlightened "this" issue a bit.
"self" is a pretty common construct, that's true. I think I've seen this() in a few languages as well... When I answer questions on the list, I tend to answer with a specific solution. In some instances, this is just what the person wants... at other times, the person is really looking for more general guidance about finding that kind of solution. It's hard to tell sometimes which way to answer :)
1.What are the rules for using quotes in dtml?
<dtml-var foo> is equivalent to <dtml-var name="foo"> <dtml-var "foo"> is equivalent to <dtml-var expr="foo"> So, what does this mean to you? If you are using <dtml-var> and you put the argument in quotes, you are actually specifying an expression. This expression needs to follow the basic structure of an expression in Python. (That's one place where knowing python will help you). So, you'll generally put a pythonish expression in quotes. <dtml-if "this() == PARENTS[0]"> essentially evalutes the this() == PARENTS[0] part as a python expression. Within the context of a <dtml-in>, this() will return the object that you have iterated to. PARENTS[0] is equivalent to self. You *could* do this: at the beginning of the method, put <dtml-call "REQUEST.set('self', this())"> Then, you could do: <dtml-if "this() == self"> which may seem clearer. But, the PARENTS[0] thing will work just dandy.
2.The nitty gritty of passing, comparing and setting variables in less than 6 hours.
DTML is an environment that is restricted for the sake of security... that may make it seem difficult to set variables, etc. But it is not really that bad. Comparing variables is pretty easy: <dtml-if "foo == bar"> will do a comparison of foo and bar, looking them both up in the namespace. Those variables could have been set by you, could have been passed in via a form, etc. To set a variable, use <dtml-call "REQUEST.set('variable', value)">. Note that this is in quotes, so you need to follow the python rules of expressions. You can put strings in single quotes, numbers don't need any quoting. You can even make a list, <dtml-call "REQUEST.set('mylist', ['foo', 'bar'])"> What if you want to change the variable? You are not allowed to do "foo = 2" in DTML. Let's say that you set a variable called "speed" to 55, and you want to add 20 to it. You would have to do <dtml-call "REQUEST.set('speed', speed + 20)">. If you have a variable that is a list, you can append to it like so: <dtml-call "mylist.append('nextelem')"> You can also set variables temporarily using the <dtml-let> tag. These variables only exist between the <dtml-let> and </dtml-let> To pass variables to another DTML Method, <dtml-call "mymethod(_.None, _, var1=5, var2='hello')"> The first two parameters pass along the namespace. After that, you can set other variables that are needed by the method.
3.How does one properly cross the chasm from rendering variables to evaluating them in expressions.
Hopefully the previous info will help. Basically, once you know python expression syntax, you shouldn't really have a problem. If you know python expression syntax *and* understand how the namespace is set up, you'll really be all set... Kevin
When I answer questions on the list, I tend to answer with a specific solution. In some instances, this is just what the person wants... at other times, the person is really looking for more general guidance about finding that kind of solution. It's hard to tell sometimes which way to answer :)
Thanks for wonderful guidance. I had no problem with your answer. My frustration was with dtml in particular. What particularly confused me in my initial effort was that i could render the values of two variables with the same values but when i compared these variables in an expression nothing happend eg: <dtml-with objectValues> <dtml-var URL> // renders a URL mysite/folder <dtml-var absolute_url> //render absolute_url mysite/folder <dtml-if "URL==absolute_url"> // even if the values where the same it always did the else part do something <dtml-else> do something else </dtml-if> </dtml-with>
Comparing variables is pretty easy:
Comparing variables inside dtml-with statements seem to be more tricky though. Am I correct in saying that when i use a dtml-with statements it pushes the object i'm working with to the top of the namespace? Many thanks Roché Compaan
At 08:53 08/11/99 , Roché Compaan wrote:
When I answer questions on the list, I tend to answer with a specific solution. In some instances, this is just what the person wants... at other times, the person is really looking for more general guidance about finding that kind of solution. It's hard to tell sometimes which way to answer :)
Thanks for wonderful guidance. I had no problem with your answer. My frustration was with dtml in particular. What particularly confused me in my initial effort was that i could render the values of two variables with the same values but when i compared these variables in an expression nothing happend eg: <dtml-with objectValues> <dtml-var URL> // renders a URL mysite/folder <dtml-var absolute_url> //render absolute_url mysite/folder <dtml-if "URL==absolute_url"> // even if the values where the same it always did the else part do something <dtml-else> do something else </dtml-if> </dtml-with>
This stems from the fundamental difference between <dtml-val absolute_url> (not quoted) and <dtml-var "absolute_url"> (quoted). As Kevin pointed out, the first is the equivalent of <dtml-var name="absolute_url">, while the second is the same as <dtml-var expr="absolute_url">. If you reference an object or method or a variable by it's name (non-quoted or name attribute), Zope will figure out if it is a callable method, and will call it for you if it is. It will also provide a context for DTML Methods and Documents when calling them. Inside a Python reference, you will have to do this figuring out yourself, a callable object or method will not be called automatically anymore. With a method like absolute_url this is easy: <dtml-var "absolute_url()"> now returns the same as <dtml-var absolute_url>. With a DTML Method or Document you will have to pass in some context however. You do this by passing in a 'client' (_.None wil do here, don't ask), and a namespace, which is _: <dtml-var "MyDTMLMethod(_.None, _, anyotherarguments=gohere) If you want to replicate the name="" behaviour in your Python expressions, you can use do a namespace lookup like <dtml-var "_['absolute_url']">. The getitem method will do the same, only now you can specify wether or not you want the object to be called if it is callable: <dtml-var "_.getitem('absolute_url', 0)"> will return the method itself, not it's result. Make the 0 a 1, and it is called again. Hope this clears things up a bit. -- 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 (3)
-
Kevin Dangoor -
Martijn Pieters -
Roché Compaan