[Templates] manipulating lists inside MACRO not the same as outside
Francesc Romŕ i Frigolé
francesc.roma@gmail.com
Wed, 6 Dec 2006 02:32:44 +0100
------=_Part_25748_3272005.1165368764670
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Thank you very much Sergey and Paul for your detailed explanations. I
understand why the macros behave the way they do now.
Francesc
On 12/6/06, Sergey Martynoff <sergey@martynoff.info> wrote:
>
>
>
> > I'm new at TT. After RTFMing i've tried to do macros that manipulate
> > lists and I've realized that I haven't understood how TT macros work.
> > Could you please tell me why the following constructs that look
> > equivalent to me behave so differently? What have I missed?
>
> Macros are just blocks (or templates) with simplified calling syntax. You
> should ot treat them as "functions" in prorgamming language.
>
>
> > 1) MACRO CAN'T RETURN A LIST: I can't use a macro that expands to a
> > list in a loop. see this simple example:
>
> Yes, MACRO blocks can return only text, not compex objects. The same about
> BLOCK directives and calling other templates via INCLUDE or PROCESS. There
> is no (legal) way to return hash or array reference.
>
>
> > 2) LIST MANIPULATION: straightforward list manipulation is ignored
> > inside MACRO, i need to resort to Virtual Methods. example list = [
> > 1..5] doesn't work, I have to do list.splice + list.push inside a loop
> > like shown below:
>
> That's because of localization of global variables I MACRO blocks. Each
> time
> you call macro, all global variables get copied, and restore their values
> after macro has finished. This localization is needed to allow recursive
> macro calls. However, localization is not "deep", so you can modify any
> object (such as your list) by reference.
>
>
> > [%
> > MACRO initialize_simple BLOCK;
> > list = [ 6..10 ] ;
> > END;
> > %]
>
> Try this as simplier way to replace list without creating new reference
> (not
> tested, but should work):
>
> [%
> MACRO initialize_simple BLOCK;
> CALL list.splice( 0, list.size, [ 6 .. 10 ] );
> END;
> %]
>
>
> --
> Sergey Martynoff
>
>
> _______________________________________________
> templates mailing list
> templates@template-toolkit.org
> http://lists.template-toolkit.org/mailman/listinfo/templates
>
------=_Part_25748_3272005.1165368764670
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
Thank you very much Sergey and Paul for your detailed explanations. I understand why the macros behave the way they do now.<br><br>Francesc<br><br><br><br><div><span class="gmail_quote">On 12/6/06, <b class="gmail_sendername">
Sergey Martynoff</b> <<a href="mailto:sergey@martynoff.info">sergey@martynoff.info</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br><br>> I'm new at TT. After RTFMing i've tried to do macros that manipulate<br>> lists and I've realized that I haven't understood how TT macros work.<br>> Could you please tell me why the following constructs that look
<br>> equivalent to me behave so differently? What have I missed?<br><br>Macros are just blocks (or templates) with simplified calling syntax. You<br>should ot treat them as "functions" in prorgamming language.
<br><br><br>> 1) MACRO CAN'T RETURN A LIST: I can't use a macro that expands to a<br>> list in a loop. see this simple example:<br><br>Yes, MACRO blocks can return only text, not compex objects. The same about<br>BLOCK directives and calling other templates via INCLUDE or PROCESS. There
<br>is no (legal) way to return hash or array reference.<br><br><br>> 2) LIST MANIPULATION: straightforward list manipulation is ignored<br>> inside MACRO, i need to resort to Virtual Methods. example list = [<br>> 1..5] doesn't work, I have to do
list.splice + list.push inside a loop<br>> like shown below:<br><br>That's because of localization of global variables I MACRO blocks. Each time<br>you call macro, all global variables get copied, and restore their values
<br>after macro has finished. This localization is needed to allow recursive<br>macro calls. However, localization is not "deep", so you can modify any<br>object (such as your list) by reference.<br><br><br>> [%
<br>> MACRO initialize_simple BLOCK;<br>> list = [ 6..10 ] ;<br>> END;<br>> %]<br><br>Try this as simplier way to replace list without creating new reference (not<br>tested, but should work):<br><br>
[%<br> MACRO initialize_simple BLOCK;<br> CALL list.splice( 0, list.size, [ 6 .. 10 ] );<br> END;<br>%]<br><br><br>--<br>Sergey Martynoff<br><br><br>_______________________________________________<br>templates mailing list
<br><a href="mailto:templates@template-toolkit.org">templates@template-toolkit.org</a><br><a href="http://lists.template-toolkit.org/mailman/listinfo/templates">http://lists.template-toolkit.org/mailman/listinfo/templates
</a><br></blockquote></div><br><br>
------=_Part_25748_3272005.1165368764670--