We already thought of this solution. Our team agreed that this is not really 
the "PHPTAL"-way and we should use slots.
But now, with your explanation of the technical issue regarding variable scope 
and the added explanation in the manual, it's fine :)

Thank you!



-----Ursprüngliche Nachricht-----
Von: phptal-boun...@lists.motion-twin.com 
[mailto:phptal-boun...@lists.motion-twin.com] Im Auftrag von Kornel Lesinski
Gesendet: Freitag, 25. Juni 2010 12:07
An: Template Attribute Language for PHP
Betreff: Re: [PHPTAL] Variables not found while filling slot

On 24-06-2010 at 18:55:31 Per Bernhardt <p...@webfactory.de> wrote:

> taken the following macro and macro-usage:
>
> <tal:block metal:define-macro="ul">
>       <ul>
>               <li tal:repeat="item items">
>                       <tal:block metal:define-slot="li" />
>               </li>
>       </ul>
> </tal:block>
>
> <tal:block tal:define="items php:array('a', 'b', 'c')"  
> metal:use-macro="ul">
>       <tal:block metal:fill-slot="li">
>               ${item}
>       </tal:block>
> </tal:block>
>
> I would expect the template to be rendered like this:
> <ul>
>       <li>a</li>
>       <li>b</li>
>       <li>c</li>
> </ul>
>
> But with the current stable and beta what you get is a 
> PHPTAL_VariableNotFoundException saying "Unable to find variable 'item'
> in current scope.
> More generally spoken: Variables defined within a macro definition 
> cannot be accessed while using the macro and filling one of its slots.
>
> Is this a bug, missing implementation or an unwanted feature?

That's just how it works. Slots work by assigning generated HTML to a hidden 
variable. They're executed immediately when fill-slot is called.


Technically the issue is which scope (variables) slot should "see":

<div tal:define="x '1'" metal:fill-slot="slot">${x}</div>

<div tal:define="x '2'" metal:define-slot="slot"><!-- 1 or 2? --></div>

For slots the answer is 1. Analogous code with macro callbacks would give 2.


I've added explanation to the manual:
http://phptal.org/manual/en/split/metal-use-macro.html#macro-callback

See also:
http://www.mail-archive.com/phptal@lists.motion-twin.com/msg01331.html

--
regards, Kornel

_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

_______________________________________________
PHPTAL mailing list
PHPTAL@lists.motion-twin.com
http://lists.motion-twin.com/mailman/listinfo/phptal

Reply via email to