in reply to HTML::TEMPLATE Simutaneous Templates

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

Replies are listed 'Best First'.
Re^2: HTML::TEMPLATE Simutaneous Templates
by StrangeDucks (Initiate) on Aug 09, 2019 at 12:49 UTC

    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