I have a template (file2.tt) calling a BLOCK (file1blk) within a sub-template ( file1.tt). The file1blk builds a hash data structure. When file2 calls file1.tt/file1blk, it seems like I cannot access the data structure itself. Text returns seem to work.
You are perfectly right :) Text returns from method calls and/or INCLUDE's do work, but no reference could be returned. This is how TT2 is designed, and this is (probably) a feature - anyway, templates are primarily for text processing, not for modifying complex data structures :)
 
 
The reason why returning reference does not work is that all returned values are literally joint in one string variable called $output. At the start of block block processing is is initialized with empty string ($output = '') and most of TT directives just add to the output. Your plugin method call ([% MyPlugin.modifythehash(MyH) %]) is converted to the following perl code:
 
$output .=  $stash->get(['MyPlugin', 0, 'modifythehash',  [ $stash->get('MyH') ]]);
 
This literal concatenation breaks the reference.
 
 
What to do? There's no reasonable way to pass complex data structure as return value. Instead, you may set a variable with some predefined name in you block, and call block processing with PROCESS directive (instead of INCLUDE, which localises all variables). You will get something like this:
 
[%
BLOCK myBlock;
  USE MyPlugin;
  MyH = {
    text1 => "Some text 1",
    text2 => "Some text 2",
  };
  MyPlugin.modifythehash( MyH );
END;
%]
 
[%
# later
PROCESS myBlock;
# now use MyH variable!
USE Dumper;
Dumper.dump( MyH );
%]
 
 

--
Sergey Martynoff

Reply via email to