As others have mentioned, HTML::Template does not support recursion. However, with a very simple change in the HTML::Template source code you can get it to support recursion to a fixed depth!

You can use TMPL_INCLUDE to recursively include itself. When you do this, HTML::Template will die with the following error:

HTML::Template->new() : likely recursive includes - parsed 10 files deep and giving up (set max_includes higher to allow deeper recursion).

So it will only allow 10 levels of include files. But it is configurable with the 'max_includes' parameter that is passed to the constructor.

So all you really need to do to get recursion to work to a given depth is to edit HTML::Template so that is doesn't die when it reaches the 'max_includes' limit. Then when you create the HTML::Template opbject, tell it how many levels deep you want your recursion to go (using max_includes), and then use a template file that uses TMPL_INCLUDE to call itself.

I won't vouch for the efficiency of this method, as each included file is most likely duplicated in memory (ie if you recurse a max of 20 times, you will have 20 parsed copies of the template in memory).

Another way to handle recursion in HTML::Template is to handle it in the code by recursively creating new HTML::Template objects and including the results in other HTML::Template objects using TMPL_VARs.

sub recurse_template { my $template_name = shift; my $template = new HTML::Template(filename => $template_name); my $value = recurse_template('template.tmpl'); $template->params(param => $value); return $template->output(); }

Of course that is an incomplete example, as you would need to stop the recursion at some point, and you probably want to add some parameters at some point... Also, this isn't very efficient either since you are creating and destroying many objects.


In reply to Re: can you repeat a character N times using HTML::Template? by cees
in thread can you repeat a character N times using HTML::Template? by spditner

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.