Howto test if macro evaluates to nothing
Hi! I would like to know wether there is a way to test if a macro evaluates to nothing. I tried the following: <div tal:condition="here/my_macros/macros/macro_name" metal:use-macro="here/my_macros/macros/macro_name"> </div> But the macro returns a tree of lists and tuples. Regards, Florian Schulze
Florian Schulze wrote:
I would like to know wether there is a way to test if a macro evaluates to nothing.
I tried the following: <div tal:condition="here/my_macros/macros/macro_name" metal:use-macro="here/my_macros/macros/macro_name"> </div>
But the macro returns a tree of lists and tuples.
I thnk you're misusing the macro feature here. Why does you macro substitution end up in not generating any output in certain circumtances? cheers, Chris -- Simplistix - Content Management, Zope & Python Consulting - http://www.simplistix.co.uk
On Wed, 10 Mar 2004 14:30:24 +0000, Chris Withers <lists@simplistix.co.uk> wrote:
Florian Schulze wrote:
I would like to know wether there is a way to test if a macro evaluates to nothing.
I tried the following: <div tal:condition="here/my_macros/macros/macro_name" metal:use-macro="here/my_macros/macros/macro_name"> </div>
But the macro returns a tree of lists and tuples.
I thnk you're misusing the macro feature here. Why does you macro substitution end up in not generating any output in certain circumtances?
cheers,
Chris
It's the normal way it works in plone. There the macro portlet_fetcher calls several macros of which some might be empty, because the user is not authenticated or there is just nothing to show. I would like to add some markup around those portlets and currently I have to edit each portlet macro, because the markup shouldn't be there when the macro is empty. Thanks and regards, Florian Schulze
Florian Schulze wrote at 2004-3-9 11:44 +0100:
I would like to know wether there is a way to test if a macro evaluates to nothing.
I tried the following: <div tal:condition="here/my_macros/macros/macro_name" metal:use-macro="here/my_macros/macros/macro_name"> </div>
But the macro returns a tree of lists and tuples.
Sure, this is the macro code. It is unlikely to be empty... When you want to check, whether the result of macro rendering is empty, then you must render the macro and look at the result. In the trivial case (the macro is independent of arguments defined in the caller), you can wrap your macro use in a PageTemplate, call this template and check its result. When your macro needs arguments, you must pass them to the template. -- Dieter
On Thu, Mar 11, 2004 at 11:23:19AM +0100, Lennart Regebro wrote:
But the macro returns a tree of lists and tuples.
That sounds like something you shoul use pyhon for, not ZPT.
I think you misunderstood the original message. These "lists and tuples" ARE the macro. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's SUPERLATIVE SENSEI! (random hero from isometric.spaceninja.com)
On Wed, Mar 10, 2004 at 09:54:07PM +0100, Dieter Maurer wrote:
Florian Schulze wrote at 2004-3-9 11:44 +0100:
I would like to know wether there is a way to test if a macro evaluates to nothing.
I tried the following: <div tal:condition="here/my_macros/macros/macro_name" metal:use-macro="here/my_macros/macros/macro_name"> </div>
But the macro returns a tree of lists and tuples.
Sure, this is the macro code. It is unlikely to be empty...
When you want to check, whether the result of macro rendering is empty, then you must render the macro and look at the result. In the trivial case (the macro is independent of arguments defined in the caller), you can wrap your macro use in a PageTemplate, call this template and check its result. When your macro needs arguments, you must pass them to the template.
But the macro is unlikely to produce literally nothing... usually there will be at least a bunch of whitespace and/or some empty tags. At the least you will have to write the macro so that all its tags are replaced with a blank value when appropriate, call the macro in a wrapper template as Dieter suggests, and then in the final template that calls the wrapper, strip() the result. I have to wonder if there's a cleaner design that will get the end result you want. Remember that macros are evaluated before TAL. I think you could set a global variable in the macro and check it in the calling template... but I haven't tried. Or maybe the variables needed by the macro could be retrieved by the template that uses the macro, e.g. from a python script. -- Paul Winkler http://www.slinkp.com Look! Up in the sky! It's THE SPOOGE! (random hero from isometric.spaceninja.com)
On Thu, 11 Mar 2004 10:34:28 -0500, Paul Winkler <pw_lists@slinkp.com> wrote:
On Wed, Mar 10, 2004 at 09:54:07PM +0100, Dieter Maurer wrote:
Florian Schulze wrote at 2004-3-9 11:44 +0100:
I would like to know wether there is a way to test if a macro evaluates to nothing.
I tried the following: <div tal:condition="here/my_macros/macros/macro_name" metal:use-macro="here/my_macros/macros/macro_name"> </div>
But the macro returns a tree of lists and tuples.
Sure, this is the macro code. It is unlikely to be empty...
When you want to check, whether the result of macro rendering is empty, then you must render the macro and look at the result. In the trivial case (the macro is independent of arguments defined in the caller), you can wrap your macro use in a PageTemplate, call this template and check its result. When your macro needs arguments, you must pass them to the template.
But the macro is unlikely to produce literally nothing... usually there will be at least a bunch of whitespace and/or some empty tags. At the least you will have to write the macro so that all its tags are replaced with a blank value when appropriate, call the macro in a wrapper template as Dieter suggests, and then in the final template that calls the wrapper, strip() the result.
I did it like that. A simple pagetemplate as a wrapper and the result is striped. This works.
I have to wonder if there's a cleaner design that will get the end result you want. Remember that macros are evaluated before TAL. I think you could set a global variable in the macro and check it in the calling template... but I haven't tried. Or maybe the variables needed by the macro could be retrieved by the template that uses the macro, e.g. from a python script.
The problem is, that I don't know which portlets will be rendered and which not, so the above solution is enough in this case and it doesn't seem like the additional page template call slows this down too much. The call result is saved in a define and then used in the condition and the rendering. Florian
participants (5)
-
Chris Withers -
Dieter Maurer -
Florian Schulze -
Lennart Regebro -
Paul Winkler