[Templates] manipulating lists inside MACRO not the same as outside

Francesc Romŕ i Frigolé francesc.roma@gmail.com
Tue, 5 Dec 2006 23:42:00 +0100


Hello list,

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?


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:

[% list = [1..5] %]
[%
  MACRO macro_list BLOCK;
     list;
   END;
%]

list:
[%
  FOREACH element IN list ;
    "$element ";
  END;
%]

macro:
[%
  FOREACH element IN macro_list ;
    "$element ";
  END;
%]


Which gives the following output ( I would expect the same output in
both cases )

list:
1 2 3 4 5

macro:
ARRAY(0x843900c)




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:

[%
   MACRO initialize_push BLOCK;
     devnull = list.splice(0);
     FOREACH num IN [ 10..15 ] ;
        list.push(num) ;
     END;
   END;
%]
[%
   MACRO initialize_simple BLOCK;
     list = [ 6..10 ] ;
   END;
%]
[%
   MACRO show_list BLOCK;
      FOREACH element IN list;
    "$element ";
      END;
   END;
%]
manual initialization
[%
   list = [ 1..5 ];
   show_list;
%]
initialization with macro
[%
   initialize_simple;
   show_list;
%]
initialization with push macro
[%
   initialize_push;

   show_list;
%]

The output of this macro is

manual initialization
1 2 3 4 5
initialization with macro
1 2 3 4 5
initialization with push macro
10 11 12 13 14 15

And I had expected

manual initialization
 1 2 3 4 5
 initialization with macro
6 7 8 9 10
 initialization with push macro
 10 11 12 13 14 15



Thank you very much for your time,
Francesc