indirect calling of methods with parameters
Sorry, but this is above me head! I'm trying to call a method that needs parameters indirectly. Well, after reading some docs i believe to somehow understand how indirect calling of variables works. What's more, everything is fine when no parameters are involved: <dtml-let method="SomeMethod"> <dtml-var method> </dtml-let> <dtml-let method="'SomeMethod'"> <dtml-var "_[methode]"> </dtml-let> Both Variants call SomeMethod, which is fine! On the other hand i'm totally lost when parameters are involved. I for example tried to build my commandcall as a string. But using _[] won't work as it naturally won't call the method but look for an object with a kind of unusal name, which doesn't exist. It seems like i need some kind of eval construct. Any suggestions? Or for short: How do i change the following code to call method instead of giving me an KeyError on the object named ParaMethod('lalala') ? <dtml-let method="'ParaMethod(\'lalala\')'"> <dtml-var "_[method]"> </dtml-let> Thanks Bernd -- -----Bernd Worsch-----------bernd.worsch@frontsite.de--------
On Tue, Mar 27, 2001 at 11:21:18AM +0200, Bernd Worsch wrote:
I for example tried to build my commandcall as a string. But using _[] won't work as it naturally won't call the method but look for an object with a kind of unusal name, which doesn't exist. It seems like i need some kind of eval construct. Any suggestions?
Supplement: In Python this isn't a problem, as there is the eval function, but it seems eval is not available from zope for example as _.eval(whatever). Hmmm?... Bernd -- -----Bernd Worsch-----------bernd.worsch@frontsite.de--------
Bernd Worsch wrote:
Sorry, but this is above me head!
I'm trying to call a method that needs parameters indirectly. Well, after reading some docs i believe to somehow understand how indirect calling of variables works. What's more, everything is fine when no parameters are involved:
<dtml-let method="SomeMethod"> <dtml-var method> </dtml-let>
<dtml-let method="'SomeMethod'"> <dtml-var "_[methode]"> </dtml-let>
Both Variants call SomeMethod, which is fine! On the other hand i'm totally lost when parameters are involved.
I for example tried to build my commandcall as a string. But using _[] won't work as it naturally won't call the method but look for an object with a kind of unusal name, which doesn't exist. It seems like i need some kind of eval construct. Any suggestions?
Or for short: How do i change the following code to call method instead of giving me an KeyError on the object named ParaMethod('lalala') ?
<dtml-let method="'ParaMethod(\'lalala\')'"> <dtml-var "_[method]"> </dtml-let>
Thanks Bernd
--
_[name] does a name lookup as you have found. You cannot pass parameters directly through _[name] though. If you wanted to pass some parameters to an arbitrary method you could do: <dtml-var expr="_.getitem(method)(params)"> If you wanted the parameter list to be variable as well, you could try passing a dictionary like so: <dtml-let params="{'arg1':value1, 'arg2':value2, ...}"> <dtml-var expr="_.getitem(method)(kw=params)"> </dtml-let> The params dictionary could be set anywhere before the actual call or even passed whole as a parameter to the calling method. -- | Casey Duncan | Kaivo, Inc. | cduncan@kaivo.com `------------------>
On Tue, Mar 27, 2001 at 09:23:53AM -0700, Casey Duncan wrote:
<dtml-var expr="_.getitem(method)(params)">
If you wanted the parameter list to be variable as well, you could try passing a dictionary like so:
<dtml-let params="{'arg1':value1, 'arg2':value2, ...}"> <dtml-var expr="_.getitem(method)(kw=params)"> </dtml-let>
The params dictionary could be set anywhere before the actual call or even passed whole as a parameter to the calling method.
Ahhhh! Ok, i simply didn't recognize that getitem allows to pass parameters. This hint provided, i solved my problem like this: <dtml-let method="'SomeMethod'" para1="'FirstArgument'" para2="'SecondArgument'"> <dtml-var expr="_.getitem(method)(para1, para2)"><br> </dtml-let> I somehow failed to achive the same by using a parameter dictionary as you suggested. Nevertheless i'm REALLY grateful for you braking my deadlock of thought! Btw i couldn't find to many docs on using getitem like this! Could be i was to dumb looking for it, but i'm quite sure it is neither in Zope Book nor in the Howto on Advanced Scripting. (Not to mention the online help). I'll be preparing a supplement and try not include to much nonsense. If anybody whishes to review or contribute i could put the supplement on zwiki.org before sending it to someone to add it to the documentation. -- -----Bernd Worsch-----------bernd.worsch@frontsite.de--------
Bernd Worsch wrote:
On Tue, Mar 27, 2001 at 09:23:53AM -0700, Casey Duncan wrote:
<dtml-var expr="_.getitem(method)(params)">
If you wanted the parameter list to be variable as well, you could try passing a dictionary like so:
<dtml-let params="{'arg1':value1, 'arg2':value2, ...}"> <dtml-var expr="_.getitem(method)(kw=params)"> </dtml-let>
The params dictionary could be set anywhere before the actual call or even passed whole as a parameter to the calling method.
Ahhhh! Ok, i simply didn't recognize that getitem allows to pass parameters. This hint provided, i solved my problem like this:
<dtml-let method="'SomeMethod'" para1="'FirstArgument'" para2="'SecondArgument'"> <dtml-var expr="_.getitem(method)(para1, para2)"><br> </dtml-let>
I somehow failed to achive the same by using a parameter dictionary as you suggested. Nevertheless i'm REALLY grateful for you braking my deadlock of thought! Btw i couldn't find to many docs on using getitem like this! Could be i was to dumb looking for it, but i'm quite sure it is neither in Zope Book nor in the Howto on Advanced Scripting. (Not to mention the online help). I'll be preparing a supplement and try not include to much nonsense. If anybody whishes to review or contribute i could put the supplement on zwiki.org before sending it to someone to add it to the documentation.
The critical difference is the _[name] calls the object (if it is callable) first before returning the result whereas _.getitem(name) does not, it simply returns the method object itself. I believe this is mentioned in Dieter's book, along with many other extremely helpful tidbits. BTW: you could shorten the above code to just: <dtml-var expr="_.getitem(method)(para1='FirstArgument', para2='SecondArgument')"><br> -- | Casey Duncan | Kaivo, Inc. | cduncan@kaivo.com `------------------>
Bernd Worsch wrote:
<dtml-let method="'ParaMethod(\'lalala\')'"> <dtml-var "_[method]"> </dtml-let>
I haven't used Zope in a while, but I believe that what you need is actually a combination of all of the things you have tried. When you use dtml-var without quotes, the dtml interpreter lookes up the name that you have specified, and then calls that object if it is callable, otherwise it calls str() or repr() on it and inserts the results (I believe. I am guessing what really happens i an object is not callable. It isn't relevant to this discussion, though). Consequently, that object gets called without any parameters, since the var tag knows nothing about any parameter list, and there is obviously not an object named 'method(param1)', just an object named 'method'. When you use the dtml-var tag with quotation marks inside, the dtml interpreter assumes the content between the quotes is a python expression, and executes it directly, within the current context. Basically, <dtml-var method> is the the equivalent of <dtml-var name="method">. <dtml-var "method()"> is the equivalent of <dtml-var expr="method()"> You should be able to say <dtml-var "ParaMethod('lalala')"> to get your example above working. Don't worry about the _[] syntax, which you don't need in this instance. Assuming your method name doesn't have any characters that would be illegal in a python expression (such as '-', or any other operator), you can call it directly from teh var tag with the syntax I've shown you. Otherwise, you can use <dtml-var "_['para-method']('lalala')"> --sam
Thanks Bernd
--
-----Bernd Worsch-----------bernd.worsch@frontsite.de--------
_______________________________________________ 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 )
-- ------------------------------------------------ "It feels much better than it ever did, much more sensitive" --John Wayne Bobbitt
On Tue, Mar 27, 2001 at 04:58:35PM -0800, sam gendler wrote:
Bernd Worsch wrote:
<dtml-let method="'ParaMethod(\'lalala\')'"> <dtml-var "_[method]"> </dtml-let>
I haven't used Zope in a while, but I believe that what you need is actually a combination of all of the things you have tried. When you use dtml-var without quotes, the dtml interpreter lookes up the name that you have specified, and then calls that object if it is callable, otherwise it calls str() or repr() on it and inserts the results (I believe. I am guessing what really happens i an object is not callable. It isn't relevant to this discussion, though). Consequently, that object gets called without any parameters, since the var tag knows nothing about any parameter list, and there is obviously not an object named 'method(param1)', just an object named 'method'.
When you use the dtml-var tag with quotation marks inside, the dtml interpreter assumes the content between the quotes is a python expression, and executes it directly, within the current context.
Basically, <dtml-var method> is the the equivalent of <dtml-var name="method">. <dtml-var "method()"> is the equivalent of <dtml-var expr="method()">
You should be able to say <dtml-var "ParaMethod('lalala')"> to get your example above working. Don't worry about the _[] syntax, which you don't need in this instance. Assuming your method name doesn't have any characters that would be illegal in a python expression (such as '-', or any other operator), you can call it directly from teh var tag with the syntax I've shown you. Otherwise, you can use <dtml-var "_['para-method']('lalala')">
--sam
Hi Sam and Thanks! Your quite right and i've been hoping to find a useful blend of technics for quite some time! I even reached the point of playing with an external method giving me python's eval command. But as i needed to use some zope objects this wasn't satisfactory either. Ok, getitem saved me for now, so i'm fine at the moment! Thanks again! Bernd -- -----Bernd Worsch-----------bernd.worsch@frontsite.de--------
sam gendler wrote:
You should be able to say <dtml-var "ParaMethod('lalala')"> to get your example above working. Don't worry about the _[] syntax, which you don't need in this instance. Assuming your method name doesn't have any characters that would be illegal in a python expression (such as '-', or any other operator), you can call it directly from teh var tag with the syntax I've shown you. Otherwise, you can use <dtml-var "_['para-method']('lalala')">
There is a mistatement in the previous paragraph. _['para-method']('lalala') would not work, as using the _[] syntax calls the object, whereas using _.getitem('para-method')('lalala') would retrieve it and then call it with the specified parameters. There are probably other errors, too, which is what happens when I get up too early and start reading email at 7am, but I am sure someone will be kind enough to point them out. --sam
participants (4)
-
Bernd Worsch -
Casey Duncan -
sam gendler -
Samuel D. Gendler