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 <[EMAIL PROTECTED]> 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
[email protected]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to