[Zope] Keeping a Variable's Value: This SHOULD Be Easy
Danny William Adair
Wed, 31 Oct 2001 09:59:20 +1300
If I understand you right, you want to keep the assigned value of a variable,
to use it again later on in your code. I guess the second call to x should
have been a simple <? echo x ?>, or you'll (probably) get two different
values(typo?). When you say that DTML *does* forget, how did you actually
assign the value?
In DTML itself, you cannot assign values to variables to be used throughout
the entire code. Of course there _are_ ways to accomplish what you want to
do, and usually one of these two options is being considered:
1) Using the <dtml-let> tag. This pushes a new variable on the namespace
stack holding the value you assigned in the tag:
The meaning of life is...<dtml-var x><br />
In case you forgot: <dtml-var x><br />
If there are no other x's in the namespace, this will generate an error:
dtml-let is a doublet tag, and the assignment is only valid within the tag.
Therefore the above code will (probably) give you an attribute error on the
last line. ("Probably" because x will be looked up through acquisition, and
there might be an object with this name)
2) Setting a REQUEST variable. The REQUEST object is a container for a couple
of useful things you have received from the client request, and also holds
some other useful information about the environment of the current request
(auto-generated variables). If you want to have a look, a dtml method holding
might be quite an enlightening thing to have around the house (call it and
you'll see what will be available to you).
>From DTML, you can read, and you can also _write_ "variables" in this object.
If you have a form with <input type="text" name="some_text">, then the DTML
method (e.g.) that you send this form to can have a look at what you have
So if you want to have a variable available "happily ever after", use:
<dtml-call "REQUEST.set('x', 6*7)">
to set it. Now your variable x is available through the REQUEST object, and
_anywhere_ beneath this assignment (or in another dtml method you are calling
inside that code) you can use <dtml-var "REQUEST[x]"> or just <dtml-var x>.
(the REQUEST[x] version, though longer, is the more precise way of looking it
up. This will even work inside a <dtml-let x="something_else"> tag.)
I believe you should have a look (at least) at the first chapters of the Zope
Book (http://www.zope.org/Members/michel/ZB), especially if you find this a
too complicated way of preserving variable values. "Good" programming style
("the Zope way programming style") might make this demand obsolete in DTML.
If you need to juggle around with variables and actually doing something to
them a couple of times, you're better off with a Python Script. (same book!)
If you need to set a lot of REQUEST variables, or have the well-known
"checkbox problem" with html forms (if not checked nothing is sent, therefore
the boolean value of the checkbox is not available), you might also want to
have a look at a product, the <dtml-set> tag:
On Wednesday 31 October 2001 08:36, you wrote:
> ...but apparently it's not. If I were writing this in PHP:
> Okay, boys and girls! Let's open the magic box and generate a random
> number! (applause)<br and drum roll>
> <? x = int mt_rand(0,9); echo x ?><br, strike up the band>
> Wow! Wasn't that exciting? Oh no! I forgot the magic number<br, shrieks of
> <? x = int mt_rand(0,9); echo x ?><br, strike up the band again> Oh, of
> course! How could I forget!<br, fade to black>
> ...but apparently DTML *does* forget. Call that daggone variable more than
> once and you have to run the formula that generates a random digit! Bam!
> Variable's gone. Is there no way around this?
> Please help...
> Zope maillist - Zope@zope.org
> ** No cross posts or HTML encoding! **
> (Related lists -
> http://lists.zope.org/mailman/listinfo/zope-dev )