[Zope] Sequences and SQL

Michel Pelletier michel@digicool.com
Tue, 20 Jul 1999 15:37:37 -0400


> -----Original Message-----
> From: Sean McMains [mailto:smcmains@nortelnetworks.com]
> Sent: Tuesday, July 20, 1999 12:50 PM
> To: 'Michel Pelletier'; 'zope@zope.org'
> Subject: RE: [Zope] Sequences and SQL
> 
> 
> I, Sean McMains, hereby promise to try EVERY STINKING THING 
> with and without
> quotes before I bug the list again.
> 
> Thanks, Michel! :) That worked beautifully.
> 

Better yet would be to learn *why* some situation require quotes and
others don't.  First, realize that quoting and not quoting are both
shorthands:

<!--#var an-object--> is short for <!--#var name=an-object-->

<!--#var "anExpression"--> is short for <!--#var expr="anExpression"-->

Referencing something *by name* does not require quotes.  In this
situation, the DTML interpreter will look up the name in the namespace,
and do it's thing with it.  For example, if the thing your referencing
is a DTML Document or Method, it will know to pass in the namespace so
that that DTML object can also take part in rendering dynamic content.
If that object is any kind of method, it will call it, passing in the
namespace.  If the object is not a method, it will take the repr() of
it.  There are several special objects that get added by DTML itself,
especially in the context of certain tags, like the #in tag; some of
these special objects being 'sequence-key' etc...

Referencing an expression is different.  In this situation, the string
in the quotes is treated like a Python expression.  If you want to call
a method, you must call() it explicitly and pass in any needed
parameters.  referencing an object, will give you that object, and if
the whole expression results in an object, #var will take the repr() of
it.  Because all of those special variables can be handy in expressions
as well as just by name, they are pushed into the '_' namespace mapping,
so in an expression, if you want to use the value of 'sequence-item' in
the expression, you can say <!--#var "_['sequence-item'] * 6"-->.  This
is because in a python expression, 'sequence-item' is literally
interpreted to mean 'sequence' minus 'item', which is not what you want.

Note that there are some side effects.  For example, if you say:

<!--#var aReallyExpensiveMethod-->

DTML will call that method *the first time you use it* and then cache
the value returned.  This way, you can reference aReallyExpensiveMethod
by name a bunch of times and not suffer the cost of running the same
method over and over, since it is, after all, really expensive.  If you
don't want to cache the value (like if you are a naughty programmer and
your really expensive method has an after effect that you want to incur
each time) you can use it in an expression:

<!--#var "aReallyExpensiveMethod(REQUEST)"-->

and DTML will not cache it.  Note that this means:

<!--#var aReallyExpensiveMethod-->
<!--#var "aReallyExpensiveMethod(REQUEST)"-->

will raise an error, because the first reference turns
aReallyExpensiveMethod into it's result (say, a sequence) and then the
second reference tries to call it, but it's no longer a method because
DTML turned it into something else (unless of course the returned object
of aReallyExpensiveMethod is itself a method, but if you've gotten
yourself into that kindof craziness you need more help than I can give
you).

-Michel

> Sean
> 
> > -----Original Message-----
> > From:	Michel Pelletier [SMTP:michel@digicool.com]
> > Sent:	Tuesday, July 20, 1999 11:07 AM
> > To:	McMains, Sean [RICH4:2V64:EXCH]; 'zope@zope.org'
> > Subject:	RE: [Zope] Sequences and SQL
> > 
> > 
> > 
> > > -----Original Message-----
> > > From: Sean McMains [mailto:smcmains@nortelnetworks.com]
> > > Sent: Tuesday, July 20, 1999 11:57 AM
> > > To: 'zope@zope.org'
> > > Subject: [Zope] Sequences and SQL
> > > 
> > > 
> > > Ok, now that I can get data into a database, I'm working on 
> > > getting it out
> > > again. A simple table works fine, but I'd like to add 
> alternate-line
> > > shading. This is what I'm trying:
> > > 
> > > <!--#in getData-->
> > > <!--#if "sequence-even"-->
> > > ...
> > 
> > It's those quotes again, man.  This time you *don't* need them.
> > 
> > Quotes cause Zope to *evaluate* a Python expression.  In this case,
> > 'sequence-even' looks to Python like 'sequence minus even'.
> > 
> > What you want is just <!--#if sequence-even-->.  Or, if you want to
> > access that value in an expression, try <!--#if 
> "_['sequence-even']"-->.
> > The '_' namespace can act like a mapping to all of these 
> DTML variables
> > that are illegal in python expressions.
> > 
> > -Michel
> > 
> > > shaded line code
> > > ...
> > > <!--#else-->
> > > ...
> > > unshaded line code
> > > ...
> > > <!--#/if-->
> > > 
> > > I can submit this to Zope no problem, but when I go to view 
> > > the page, I get
> > > the standard error message with this in the source:
> > > 
> > > <!--
> > >  Error type:  
> > >  Error value: sequence
> > >  -->
> > > 
> > > The error is generated by the line with the IF statement, as 
> > > I've verified
> > > by changing to to test a different expression. I expect that 
> > > I'm bumping
> > > into something syntactical, but can't figure out how to fix 
> > > it! Help from
> > > you more experienced Zopers is appreciated.
> > > 
> > > Thanks,
> > > Sean McMains
> > > 
> > > _______________________________________________
> > > Zope maillist  -  Zope@zope.org
> > > http://www.zope.org/mailman/listinfo/zope
> > > 
> > > (For developer-specific issues, use the companion list,
> > > zope-dev@zope.org - 
http://www.zope.org/mailman/listinfo/zope-dev )
> > 
> 
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://www.zope.org/mailman/listinfo/zope
> 
> (For developer-specific issues, use the companion list,
> zope-dev@zope.org - http://www.zope.org/mailman/listinfo/zope-dev )

_______________________________________________
Zope maillist  -  Zope@zope.org
http://www.zope.org/mailman/listinfo/zope

(For developer-specific issues, use the companion list,
zope-dev@zope.org - http://www.zope.org/mailman/listinfo/zope-dev )