|
Posted by Dave on April 8, 2005, 2:59 pm
Please log in for more thread options
Can anyone suggest a way to display an arbitrarily deep nested loop structure
using HTML::Template? Below is what I've tried.
I have a data structure that has an arbitrary number of nested loops with the
same key name of "categories" (named this way to test the idea below). An
example is this:
$template_vars-> = [
{
'categories' => [
'categories' => [
{
'name' => 'category 3:a',
},
{
'name' => 'category 3:b',
},
],
{
'name' => 'category 2:a',
},
{
'name' => 'category 2:b',
},
],
},
{
'name' => 'category 1:a',
},
{
'name' => 'category 1:b',
},
];
I want to be able to display this recursive structure regardless of how many
nested 'categories' => {} there are.
I created the following two templates:
test.template
===============
[...]
<TMPL_IF NAME="categories">
<TMPL_INCLUDE NAME="categoryHierarchy.include">
</TMPL_IF>
[...]
categoryHiearchy.include
==========================
<TMPL_LOOP NAME="categories">
<TMPL_VAR NAME="name"><br>
</TMPL_LOOP>
<TMPL_IF NAME="categories">
<TMPL_INCLUDE NAME="categoryHierarchy.include">
</TMPL_IF>
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?
|
|
Posted by Bill Karwin on April 8, 2005, 4:53 pm
Please log in for more thread options
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-> = [
{
'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.
|
|
Posted by Dave on April 8, 2005, 8:31 pm
Please log in for more thread options
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-> = [
> {
> '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.
|
| Similar Threads | Posted | | Nested loops? | November 12, 2005, 10:07 pm |
| HTML::Template | March 5, 2008, 2:40 pm |
| HTML::Template and __ODD__ | July 22, 2005, 7:48 am |
| HTML::Template not outputting | February 25, 2008, 3:11 pm |
| Mixing HTML::Template and GD::Graph | May 14, 2005, 8:53 am |
| Namespace for new module: HTML::Template::HTX? | May 13, 2005, 7:17 pm |
| HTML::Template, is it possible to nest inside a ? | June 4, 2007, 4:58 pm |
| install HTML::Template - Problem reading cache file / Bad file number | July 24, 2004, 7:55 pm |
| Recursive download from the web | February 4, 2005, 1:12 am |
| sftp loops Win32 platform | July 7, 2006, 12:28 pm |
|