[Zope] Re: Howto test if macro evaluates to nothing

Florian Schulze florian.proff.schulze at gmx.net
Thu Mar 11 11:47:00 EST 2004


On Thu, 11 Mar 2004 10:34:28 -0500, Paul Winkler <pw_lists at 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




More information about the Zope mailing list