Re: HTML::Template arbitraryily nested recursive loops



Bill,

Thanks very much. Upon further reflection after my original post, this is exactly the idea I came up with before I read your response. Since the data structure really just needs to be displayed correctly, flattening it and putting some indicator in each loop iteration appears to be the only solution. It's an ugly workaround, but it will get the job done. Thanks for validating my thinking.

Dave


Bill Karwin wrote:
Dave wrote:

I've tried setting max_includes to a very high number and to 0, but regardless of its value, this doesn't work. If it is set at 0, the cgi that calls the templates above runs infinitely, and if max_includes is any other number, I get the error:

HTML::Template->new() : likely recursive includes - parsed 10 files deep and giving up (set max_includes higher to allow deeper recursion). at /opt/perl-5.6.0-gcc-2.95.2/lib/site_perl/5.6.0/HTML/Template.pm line 2054.

Any ideas on how to make this work?


As you've seen, HTML::Template does its INCLUDE operations when it parses the file, during the new() method. It doesn't defer the includes until you've fed all the outputs and ask to render the page.

You probably can't feed it an arbitrarily deep nested data structure. You have to feed it a flatter data structure, and at each level give it some additional parameter to cause the HTML table to indent appropriately.

I did this once using dashes to indicate indentation level:

$template->vars->{'categories'} = [
  {
    'name' => 'category 1:a',
    'depthlevels' = []
  }
  {
    'name' => 'category 2:a',
    'depthlevels' = [ 'x' ]
  }
  {
    'name' => 'category 3:a',
    'depthlevels' = [ 'x', 'x' ]
  }
];

The 'x' characters are just placeholders, to force the loop to be of the correct length.

<TD <TMPL_LOOP NAME=depthlevels>-</TMPL_LOOP> <TMPL_VAR NAME=name> </TD>

Or you could do it with an integer that you give to COLSPAN in your template or something:

<TMPL_LOOP NAME="categories">
  <TD COLSPAN=<TMPL_VAR NAME=depth>> <TD><TMPL_VAR NAME=name></TD>
</TMPL_LOOP>

Obviously that example is incomplete as a solution, because you need to establish the non-spanned cells first, and you might need to give a complementary colspan value for the category name. But you get the idea.

Regards,
Bill K.
.