Asking for the type of a variable
It looks like I need to ask for the type of a python variable from DTML. So far, I haven't been able to get this to work: <dtml-if "type(TAG)==type('x')"> I am trying to see if the TAG variable is a string. Zope complains about "type". I tried using the special name space: <dtml-if "_.type(TAG)==_.type('x')"> But it looks like "type" is not a function available through that means. Is there another way to use the type of a variable for branching? Actually, the reason this arises is forms. I have a series of radio buttons in a form. When submitted to Zope, I notice that if one item is selected, the value is a string. When many items are selected, the value is a list of strings. Since <dtml-in> does not accept a single string as an argument, I need to test to see if the incoming value is a string or a list of strings before processing it. I will gladly accept any better idioms for handling this case. The tests certainly make the DTML code more complex than I think it should be. David S. Harrison (dsh@magma-da.com)
"David S. Harrison" wrote:
Actually, the reason this arises is forms. I have a series of radio buttons in a form. When submitted to Zope, I notice that if one item is selected, the value is a string. When many items are selected, the value is a list of strings.
then it's most likely a checkbox ?
Since <dtml-in> does not accept a single string as an argument, I need to test to see if the incoming value is a string or a list of strings before processing it. I will gladly accept any better idioms for handling this case.
set the name to myradiobutton:list , i.e <input type=checkbox name="cb:list" value="thevalue"> or for numeric values <input type=checkbox name="cb:list:int" value=7>
The tests certainly make the DTML code more complex than I think it should be.
Sure ;) -------------- Hannu
Hannu Krosing wrote:
set the name to myradiobutton:list , i.e
<input type=checkbox name="cb:list" value="thevalue">
Interesting. I didn't see any coverage for this kind of thing in the content guide or the DTML guide. Is there someplace else I should look to see what kinds of things are supported as types in forms? David S. Harrison (dsh@magma-da.com)
"David S. Harrison" wrote:
Hannu Krosing wrote:
set the name to myradiobutton:list , i.e
<input type=checkbox name="cb:list" value="thevalue">
Interesting. I didn't see any coverage for this kind of thing in the content guide or the DTML guide. Is there someplace else I should look to see what kinds of things are supported as types in forms?
For some obscure reason this info is hidden in Zope SQL Methods manual more exactly in file ZSQL.1.4.2.html ;) ------------ Hannu
On Tue, 9 Nov 1999, David S. Harrison wrote:
set the name to myradiobutton:list , i.e
<input type=checkbox name="cb:list" value="thevalue">
Interesting. I didn't see any coverage for this kind of thing in the content guide or the DTML guide. Is there someplace else I should look to see what kinds of things are supported as types in forms?
There is a HowTo on this, stolen from old Documentation by Jim Fulton: http://www.zope.org/Members/Zen/howto/FormVariableTypes ___ // Zen (alias Stuart Bishop) Work: zen@cs.rmit.edu.au // E N Senior Systems Alchemist Play: zen@shangri-la.dropbear.id.au //__ Computer Science, RMIT WWW: http://www.cs.rmit.edu.au/~zen
At 22:55 9-11-99 , David S. Harrison wrote:
Actually, the reason this arises is forms. I have a series of radio buttons in a form. When submitted to Zope, I notice that if one item is selected, the value is a string. When many items are selected, the value is a list of strings. Since <dtml-in> does not accept a single string as an argument, I need to test to see if the incoming value is a string or a list of strings before processing it. I will gladly accept any better idioms for handling this case. The tests certainly make the DTML code more complex than I think it should be.
I take it you mean checkboxes. Just append the :list modifier to the checkbox name, and it will always be a list, wether the user selected zero, one, or more checkboxes: <input type=checkbox name="publications:list" value="Magazine"> Magazine <br> <input type=checkbox name="publications:list" value="Book"> Book <br> <input type=checkbox name="publications:list" value="eZine"> EZine (email magazine) <br> -- Martijn Pieters, Web Developer | Antraciet http://www.antraciet.nl | T: +31 35 7502100 F: +31 35 7502111 | mj@antraciet.nl http://www.antraciet.nl/~mj | PGP: http://wwwkeys.nl.pgp.net:11371/pks/lookup?op=get&search=0xA8A32149 ---------------------------------------------
On Tue, 9 Nov 1999, David S. Harrison wrote:
Actually, the reason this arises is forms. I have a series of radio buttons in a form. When submitted to Zope, I notice that if one item is selected, the value is a string. When many items are selected, the value is a list of strings. Since <dtml-in> does not accept a single string as an argument, I need to test to see if the incoming value is a string or a list of strings before processing it. I will
You can just append :list to your variable in the Form and it will automatically get converted to a list. Then you can use <dtml-in ...> even for one item. Pavlos
You need to look at: http://www.zope.org/Members/Zen/howto/FormVariableTypes Use :list to make your form field a list, no matter how many entries it has. -- Loren
-----Original Message----- From: zope-dev-admin@zope.org [mailto:zope-dev-admin@zope.org]On Behalf Of David S. Harrison Sent: Tuesday, November 09, 1999 13:55 To: zope-dev@zope.org Subject: [Zope-dev] Asking for the type of a variable
It looks like I need to ask for the type of a python variable from DTML. So far, I haven't been able to get this to work:
<dtml-if "type(TAG)==type('x')">
I am trying to see if the TAG variable is a string. Zope complains about "type". I tried using the special name space:
<dtml-if "_.type(TAG)==_.type('x')">
But it looks like "type" is not a function available through that means. Is there another way to use the type of a variable for branching?
Actually, the reason this arises is forms. I have a series of radio buttons in a form. When submitted to Zope, I notice that if one item is selected, the value is a string. When many items are selected, the value is a list of strings. Since <dtml-in> does not accept a single string as an argument, I need to test to see if the incoming value is a string or a list of strings before processing it. I will gladly accept any better idioms for handling this case. The tests certainly make the DTML code more complex than I think it should be.
David S. Harrison (dsh@magma-da.com)
On 11/9/99 4:55 PM, David S. Harrison at dsh@magma-da.com wrote:
It looks like I need to ask for the type of a python variable from DTML. So far, I haven't been able to get this to work:
<dtml-if "type(TAG)==type('x')">
I am trying to see if the TAG variable is a string. Zope complains about "type". I tried using the special name space:
<dtml-if "_.type(TAG)==_.type('x')">
BTW, the reason 'type' isn't in the name space is security related and has to do with what 'type' returns (the base abstract class in Python), and the ability you could then have to modify things that you shouldn't. We've thought a lot about what would be useful, perhaps just returning the stringified version of it's class info. Dunno, ideas? Chris -- | Christopher Petrilli Python Powered Digital Creations, Inc. | petrilli@digicool.com http://www.digicool.com
Christopher Petrilli wrote:
On 11/9/99 4:55 PM, David S. Harrison at dsh@magma-da.com wrote:
[snip]
<dtml-if "_.type(TAG)==_.type('x')">
Since I've hardly ever seen 'type' used *except* in this exact kind of construction, how about a standard namespace function: def same_type(a, b): return type(a) == type(b) which would allow <dtml-if "_.same_type(TAG, 'x')">. It could even be extended to fall back to meta_type on ExtensionClasses, but it's nice and simple as is. Cheers, Evan @ 4-am
participants (8)
-
Christopher Petrilli -
David S. Harrison -
Evan Simpson -
Hannu Krosing -
Loren Stafford -
Martijn Pieters -
Pavlos Christoforou -
Stuart 'Zen' Bishop