[Zope] Calculations within a document

Stephen Pitts smpitts@midsouth.rr.com
Sat, 19 Feb 2000 11:04:57 -0600


On Sat, Feb 19, 2000 at 01:12:49PM +0100, Boris Povazay wrote:
> Since I am a "Zope-Beginner" I am currently faced with some problems -
> maybe you can help me with these.
> 
> Declarations & type-conversions:
> How are variable-declerations done right within dtml?
> Like: float x;
> or: int x; float y;
> y=float(x);
> 
> Calculations:
> How are iterative calculations with variables done correctly in a
> dtml-Method?
> like: float x, 
> x=0;
> x=x+1
> 
> Especially the type-conversion puzzles me. I`ve read several Zope-docs,
> but I have still not figured out how to do this.
> 
> 

You can't create new variables DIRECTLY in DTML. The attitude of the DC
people is "if you are creating lots of variables and doing lots of
calculations you probably should be doing it in Python." However, you
can indirectly create new variables like this:
	<dtml-call "REQUEST.set('x', 1)">
creates an int called x
	<dtml-call "REQUEST.set('y', 1.0)">
creates a float called y

DTML uses Python variable semantics, so you might want to play around in
python a bit to get a feel for the variables:
>>> f = 1
>>> f / 3
0
>>> g = 1.0
>>> g / 3
0.333333333333

In this example, f is automatically typed as an integer, while g is
automatically typed as a float. You can override this by using the float
and int methods:
>>> float(f) / 3
0.333333333333
>>> int(g) / 3
0

From DTML, you can force x to be created as a float like this:
	<dtml-call "REQUEST.set('x', _.float(1))">
Notice the _.float call. Some python features are available to DTML
through the _ object, for more info check out:
http://www.zope.org/Documentation/Guides/DTML-HTML/DTML.5.3.2.3.html

I hope this has helped, if you have any further questions feel free to
reply.
-- 
Stephen Pitts
smpitts@midsouth.rr.com
webmaster - http://www.mschess.org