StrangeDucks has asked for the wisdom of the Perl Monks concerning the following question:

I’ve been using HTML::TEMPLATE for a number of years. I now need to be able to write to two templates using the same data. Currently, I open a template and loop through parameters to create the HTML page. Can I nest two templates together and write to both of them simultaneously? Or, do I need to do a second loop for the second template?

Replies are listed 'Best First'.
Re: HTML::TEMPLATE Simutaneous Templates
by Anonymous Monk on Aug 08, 2019 at 01:59 UTC

    Hi

    What do you mean by "write to templates"?

    What do you mean by a "loop for a template"?

    Some hints below

    #!/usr/bin/perl -- use strict; use warnings; use HTML::Template; my $tmpl = q{ <TMPL_LOOP NAME=LOOP> Name: <TMPL_VAR NAME=NAME> Nicknames:<TMPL_LOOP NAME=nicknames> -: <TMPL_VAR NAME=NAME></TMPL_LOOP> </TMPL_LOOP>}; my $data = { LOOP => [ { name => 'Bobby', nicknames => [{name => 'the big bad wolf'}, {name => 'He-M +an'}], }, ], }; for(1..2){ my $template = HTML::Template->new( scalarref => \$tmpl, ); $template->param( %$data ); $template->output(print_to => *STDOUT); } __END__ Name: Bobby Nicknames: -: the big bad wolf -: He-Man Name: Bobby Nicknames: -: the big bad wolf -: He-Man

      I needlessly made the question too complex adding details that weren't relevant or remotely clear. Sorry about that. Let me rephrase the question.

      Normally, I use to create the displayed page parsing the data as necessary. What I would like to do is to create a file that can be used as the body of an email at the same time that I'm creating the displayed page. This would mean that two templates would be used at the same time. The idea to make the code easier to maintain. I hope this is a little bit more clear of my intent.

        Trivial SSCCE:

        #!/usr/bin/env perl use strict; use warnings; use HTML::Template; my @sources = ( "Hello <TMPL_VAR NAME=FOO>\n", "Goodbye <TMPL_VAR NAME=FOO>\n", ); my @templates; # Create and populate 2 templates with one loop for my $source (@sources) { my $tmpl = HTML::Template->new (scalarref => \$source); $tmpl->param (FOO => 'StrangeDucks'); push @templates, $tmpl; } # Output from the two teamplates for my $t (@templates) { print $t->output }

        See also the HTML::Template Tutorial.

        (Edit: minor update to improve generality.)

        I needlessly made the question too complex adding details that weren't relevant or remotely clear. Sorry about that. Let me rephrase the question. Normally, I use to create the displayed page parsing the data as necessary. What I would like to do is to create a file that can be used as the body of an email at the same time that I'm creating the displayed page. This would mean that two templates would be used at the same time. The idea to make the code easier to maintain. I hope this is a little bit more clear of my

        Hi,

        Sorry, it sounds as if you're not a programmer, my advice is hire a programmer