[Zope] Keeping a Variable's Value: This SHOULD Be Easy

Danny William Adair danny@adair.net
Wed, 31 Oct 2001 09:59:20 +1300


Hi Ben!

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:

-----------------------------------------
<dtml-let x="6*7">
The meaning of life is...<dtml-var x><br />
In case you forgot: <dtml-var x><br />
</dtml-let>
If there are no other x's in the namespace, this will generate an error:
<dtml-var x>
-----------------------------------------

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

<dtml-var REQUEST>

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
typed:

<dtml-var "REQUEST['some_text']">
or
<dtml-var "REQUEST.form['some_text']">

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:

http://www.zope.org/Members/ivo/SetTag

hth,
Danny

On Wednesday 31 October 2001 08:36, you wrote:
> ...but apparently it's not. If I were writing this in PHP:
>
> <html><body>
> 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
> horror>
> <? 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>
> </body></html>
>
> ...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...
> BenO
>
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )

-------------------------------------------------------