Continued problem tal:condition
Hi All Thanks once again for all the help you have given me. I have tried out the suggestions. Now all my macros are in the same page. I m am trying to use : <div tal:condition="request/form/lkid | nothing"> <tal:block define="mName python:here.oms.Users.lib.pset_links(lkid=request.form['lkid'])"> <metal:block use-macro="container/zptmac_memall/macros/?mName"> List </metal:block> </tal:block> </div> Or else : <div tal:condition="request.form.get('lkid') == 't' "> <metal:block use-macro="container/zptmac_memall/macros/zptmac_alllist"> List </metal:block> </div> In both cases the earlier error of : exceptions.KeyError on id in "<PythonExpr request.form['id'] == 't' or nothing>" The problem is that the value for "lkid" even when it is passed does not call the macro. Irrespective of whatever value i pass i cant see the macros although the error is not being shown. Why is this? I would like to state the problem again. I have a page template that on loading first should not show me any macro after i send the form variable via a link the page should get refreshed and now the pag e should also show me the macro apart from what it was showing earlier. now there are no errors being shown but i still cant view the macros after the value has been passed. Could you please help me out. Thanks and Best Regards John Kunchandy Original message:: exceptions.KeyError on id in "<PythonExpr request.form['id'] == 't' or
nothing>" -------------------------------------------------------^ Could you tell me how I can rewrite this so that the first time the page is loaded even if there is no value for "id" the page will be shown . And only if I pass a form variable it should execute.
Try replacing "request.form['id'] == 't' or nothing" with: request.form.get(id')=='t' cheers, Chris On Tue, 2002-08-20 at 22:03, KevinL wrote:
How about:
<div tal:define="macroName request/form/id"> <metal:block use-macro="container/zptmac_memtask/macros/?macroName"> List </metal:block> </div>
Combined with renaming your "id" field in your forms (or your macro names, either will do) so they match? That munges the whole list of tal:conditions down to something more manageable...
Heh. After posting this, I looked closer at your list below, and realised you're altering both the macro file name, and the name of the macro inside the file. That'd require that you be able to replace multiple elements of the macro name, rather than a single. That's not doable atm. I still reckon it should be... Then you'd be covered by a tal:define="macroName string:container/zptmac_${request/id}task/macros/zptmac_${request/id}list". Alternately, you can make all macros in macro files be called the same thing - so you end up with a file "zptmac_mempending", which has a macro called "std", and a "zptmac_memtask" which also has a macro called "std", and you just alter one element of the path to the macro, based on the id from the form. KevinL
On Tuesday, August 20, 2002, at 07:18 , zope wrote:
Hi All I am trying to execute a macro only if a condition is satisified. The code I am using is:
<div tal:condition="python:request.form['id'] == 't' | nothing" metal:use-macro="container/zptmac_memtask/macros/zptmac_taskslist"> List </div> <div tal:condition="python:request.form['id'] == 'p' | nothing" metal:use-macro="container/zptmac_mempending/macros/zptmac_pendlist"> List </div> <div tal:condition="python:request.form['id'] == 'w' | nothing" metal:use-macro="container/zptmac_memweek/macros/zptmac_weeklist"> List </div> <div tal:condition="python:request.form['id'] == 'a' | nothing" metal:use-macro="container/zptmac_memall/macros/zptmac_alllist"> List </div>
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.381 / Virus Database: 214 - Release Date: 8/2/2002
If all of your macros are in zpt_memall, and lkid is going to be either not there, or the name of one of the macros inside zpt_memall, then all you should need is: <div tal:condition="exists:request/form/lkid" tal:omit-tag=""> <metal:block use-macro="container/zptmac_memall/macros/?lkid"> </metal:block> </div>
From what you've said, that should cover it, I'd imagine. If the macro isn't being displayed when you do that, then I'd be looking closely at the macros (and the source for the pages). It should display an error, though, I'd have thought.
KevinL On Wed, 2002-08-21 at 16:09, zope wrote:
Hi All
Thanks once again for all the help you have given me. I have tried out the suggestions. Now all my macros are in the same page. I m am trying to use : <div tal:condition="request/form/lkid | nothing"> <tal:block define="mName python:here.oms.Users.lib.pset_links(lkid=request.form['lkid'])"> <metal:block use-macro="container/zptmac_memall/macros/?mName"> List </metal:block> </tal:block> </div>
Or else : <div tal:condition="request.form.get('lkid') == 't' "> <metal:block use-macro="container/zptmac_memall/macros/zptmac_alllist"> List </metal:block> </div> In both cases the earlier error of : exceptions.KeyError on id in "<PythonExpr request.form['id'] == 't' or nothing>"
The problem is that the value for "lkid" even when it is passed does not call the macro. Irrespective of whatever value i pass i cant see the macros although the error is not being shown. Why is this? I would like to state the problem again. I have a page template that on loading first should not show me any macro after i send the form variable via a link the page should get refreshed and now the pag e should also show me the macro apart from what it was showing earlier. now there are no errors being shown but i still cant view the macros after the value has been passed. Could you please help me out.
Thanks and Best Regards John Kunchandy Original message:: exceptions.KeyError on id in "<PythonExpr request.form['id'] == 't' or
nothing>" -------------------------------------------------------^ Could you tell me how I can rewrite this so that the first time the page is loaded even if there is no value for "id" the page will be shown . And only if I pass a form variable it should execute.
Try replacing "request.form['id'] == 't' or nothing" with:
request.form.get(id')=='t'
cheers,
Chris
On Tue, 2002-08-20 at 22:03, KevinL wrote:
How about:
<div tal:define="macroName request/form/id"> <metal:block use-macro="container/zptmac_memtask/macros/?macroName"> List </metal:block> </div>
Combined with renaming your "id" field in your forms (or your macro names, either will do) so they match? That munges the whole list of tal:conditions down to something more manageable...
Heh. After posting this, I looked closer at your list below, and realised you're altering both the macro file name, and the name of the macro inside the file.
That'd require that you be able to replace multiple elements of the macro name, rather than a single. That's not doable atm. I still reckon it should be... Then you'd be covered by a tal:define="macroName string:container/zptmac_${request/id}task/macros/zptmac_${request/id}list".
Alternately, you can make all macros in macro files be called the same thing - so you end up with a file "zptmac_mempending", which has a macro called "std", and a "zptmac_memtask" which also has a macro called "std", and you just alter one element of the path to the macro, based on the id from the form.
KevinL
On Tuesday, August 20, 2002, at 07:18 , zope wrote:
Hi All I am trying to execute a macro only if a condition is satisified. The code I am using is:
<div tal:condition="python:request.form['id'] == 't' | nothing" metal:use-macro="container/zptmac_memtask/macros/zptmac_taskslist"> List </div> <div tal:condition="python:request.form['id'] == 'p' | nothing" metal:use-macro="container/zptmac_mempending/macros/zptmac_pendlist"> List </div> <div tal:condition="python:request.form['id'] == 'w' | nothing" metal:use-macro="container/zptmac_memweek/macros/zptmac_weeklist"> List </div> <div tal:condition="python:request.form['id'] == 'a' | nothing" metal:use-macro="container/zptmac_memall/macros/zptmac_alllist"> List </div>
--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.381 / Virus Database: 214 - Release Date: 8/2/2002
_______________________________________________ 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 )
zope writes:
... Now all my macros are in the same page. I m am trying to use : <div tal:condition="request/form/lkid | nothing"> <tal:block define="mName python:here.oms.Users.lib.pset_links(lkid=request.form['lkid'])"> <metal:block use-macro="container/zptmac_memall/macros/?mName"> List </metal:block> This does not work, because TAL-attributes are only evaluated once all METAL-attributes have been executed.
As a consequence you must not refer to a TAL defined variable in a METAL attribute. Use a Python script to return the macro object you need. Furthermore, you cannot suppress a METAL attribute execution with a "tal:condition" (for the same reasons). Use an (almost) empty macro in such a case (it may need to define slots, when you call it with "metal:fill-slot").
... <div tal:condition="request.form.get('lkid') == 't' "> Here, a "python:" is missing.
Dieter
On Thu, 2002-08-22 at 04:30, Dieter Maurer wrote:
zope writes:
... Now all my macros are in the same page. I m am trying to use : <div tal:condition="request/form/lkid | nothing"> <tal:block define="mName python:here.oms.Users.lib.pset_links(lkid=request.form['lkid'])"> <metal:block use-macro="container/zptmac_memall/macros/?mName"> List </metal:block> This does not work, because TAL-attributes are only evaluated once all METAL-attributes have been executed.
As a consequence you must not refer to a TAL defined variable in a METAL attribute.
Use a Python script to return the macro object you need.
Furthermore, you cannot suppress a METAL attribute execution with a "tal:condition" (for the same reasons).
Use an (almost) empty macro in such a case (it may need to define slots, when you call it with "metal:fill-slot").
Um. That's wrong, I believe. I have code that does almost what he's doing above - defines skin and subSkin, which are then referenced as ?skin and ?subSkin in the metal use-macro attribute. At least, I did before I had to shift to python returning the macro... We also have code that uses a macro only if a certain condition is met (and just prints a redirect otherwise), and I'm fairly sure that works. I think he's actually having trouble with the existence or not of the form element. Could try: tal:condition = "python:request.form.get('lkid',None)" That'll allow people through if lkid exists and is non-null, but won't error if it's not there (.get() returns the value if found, or the default value (in this case None) if not found). My preferred syntax (because it dodges using python in the tag) is: tal:condition = "exists:request/form/lkid" But that will let the user through if lkid exists and is "" - may or may not be a problem to you. Inside that, the tal:define and metal calls look fine to me, presuming your pset_links() method is operating as expected (ie. taking the lkid value and returning a macro name). KJL
KevinL writes:
On Thu, 2002-08-22 at 04:30, Dieter Maurer wrote:
zope writes:
... Now all my macros are in the same page. I m am trying to use : <div tal:condition="request/form/lkid | nothing"> <tal:block define="mName python:here.oms.Users.lib.pset_links(lkid=request.form['lkid'])"> <metal:block use-macro="container/zptmac_memall/macros/?mName"> List </metal:block> This does not work, because TAL-attributes are only evaluated once all METAL-attributes have been executed.
As a consequence you must not refer to a TAL defined variable in a METAL attribute.
Use a Python script to return the macro object you need.
Furthermore, you cannot suppress a METAL attribute execution with a "tal:condition" (for the same reasons).
Use an (almost) empty macro in such a case (it may need to define slots, when you call it with "metal:fill-slot").
Um. That's wrong, I believe. It's explicitely stated in the documentation.
And, think a moment: Many tal attributes reference things available only under special circumstances, e.g. form variables. When you evaluate them in a different context, you will get exceptions. Then, think of macro expansion. When macro expansion is enabled, you expand macros outside when you look at the template source, outside of any special context. You cannot rely on tal attributes for that. Of course, it would be possible to not look at tal attributes when macro expansion is enable but look at them when it is disabled. But that would be a really strange design.... Dieter
On Fri, 2002-08-23 at 06:25, Dieter Maurer wrote:
KevinL writes:
On Thu, 2002-08-22 at 04:30, Dieter Maurer wrote:
zope writes:
... Now all my macros are in the same page. I m am trying to use : <div tal:condition="request/form/lkid | nothing"> <tal:block define="mName python:here.oms.Users.lib.pset_links(lkid=request.form['lkid'])"> <metal:block use-macro="container/zptmac_memall/macros/?mName"> List </metal:block> This does not work, because TAL-attributes are only evaluated once all METAL-attributes have been executed.
As a consequence you must not refer to a TAL defined variable in a METAL attribute.
Use a Python script to return the macro object you need.
Furthermore, you cannot suppress a METAL attribute execution with a "tal:condition" (for the same reasons).
Use an (almost) empty macro in such a case (it may need to define slots, when you call it with "metal:fill-slot").
Um. That's wrong, I believe. It's explicitely stated in the documentation.
And, think a moment:
Many tal attributes reference things available only under special circumstances, e.g. form variables. When you evaluate them in a different context, you will get exceptions.
Then, think of macro expansion. When macro expansion is enabled, you expand macros outside when you look at the template source, outside of any special context. You cannot rely on tal attributes for that.
Of course, it would be possible to not look at tal attributes when macro expansion is enable but look at them when it is disabled. But that would be a really strange design....
Dieter
I just added a page template, test_macro, and surrounded the default content with "metal:block define-macro="test". I added another ptl, test, and made it consist entirely of: <span tal:define="mName string:test"> <metal:block use-macro="here/test_macro/macros/?mName"> </metal:block> </span> When you view test, you get the test_macro macro appearing. This works. More than that, the conditional picking of macros like that is important, I think - otherwise a lot of the dynamicness of a site vanishes. Perhaps we're talking at cross-purposes? Or have I been relying on a brokenness in macros/tal? I can certainly see in the documentation that it says macros are expanded before tal is executed, but that seems to not be the case in practice? KevinL
KevinL writes:
... I just added a page template, test_macro, and surrounded the default content with "metal:block define-macro="test". I added another ptl, test, and made it consist entirely of:
<span tal:define="mName string:test"> <metal:block use-macro="here/test_macro/macros/?mName"> </metal:block> </span>
When you view test, you get the test_macro macro appearing.
This works. More than that, the conditional picking of macros like that is important, I think - otherwise a lot of the dynamicness of a site vanishes. Perhaps we're talking at cross-purposes? Or have I been relying on a brokenness in macros/tal? I can certainly see in the documentation that it says macros are expanded before tal is executed, but that seems to not be the case in practice? What happens when you turn on "Expand macros when editing"?
Dieter
Ahhh - dunno, we never use that :) We publish to Zope, rather than coding in it - all code is written and stored in cvs offline, loaded up automatically to Zope (our main app has a "refresh pages" link that cvs up's and removes/loads up all the ptl from scratch), and published from there to web. Don't really use the ZMI for much more than the ocassional refresh of a product - everything else is done programmatically. Don't think that should make that much difference, though - if "expand macros" means you can't dynamically include or exclude macros, then it seems like something we wouldn't want to do. The code below works well for what we do, and suggests (as you've pointed out) that the spec may not be entirely clear on whether tal or metal is resolved first under what conditions. KevinL On Sat, 2002-08-24 at 03:15, Dieter Maurer wrote:
KevinL writes:
... I just added a page template, test_macro, and surrounded the default content with "metal:block define-macro="test". I added another ptl, test, and made it consist entirely of:
<span tal:define="mName string:test"> <metal:block use-macro="here/test_macro/macros/?mName"> </metal:block> </span>
When you view test, you get the test_macro macro appearing.
This works. More than that, the conditional picking of macros like that is important, I think - otherwise a lot of the dynamicness of a site vanishes. Perhaps we're talking at cross-purposes? Or have I been relying on a brokenness in macros/tal? I can certainly see in the documentation that it says macros are expanded before tal is executed, but that seems to not be the case in practice? What happens when you turn on "Expand macros when editing"?
Dieter
participants (3)
-
Dieter Maurer -
KevinL -
zope