Template::Alloy also provides ways to do what you want. In addition to supporting all of HTML::Template and HTML::Template::Expr, it also allows for using TT tags in your templates as well - and as a plus Template::Alloy is faster than HTML::Template.
The following code shows inserting dynamic page content in two ways: passing a variable to the PROCESS directive, and the second way is passing raw text to eval (eval simply uses the current template object and parses the text that is passed as a template - if normal cache options are set, this method can still be very fast).
use Template::Alloy;
use POSIX qw(tmpnam);
my $file1 = tmpnam;
my $file2 = tmpnam;
END { unlink $file1 };
END { unlink $file2 };
if (open my $fh, ">", $file1) {
print $fh "I am file1 ($file1).
title = (<TMPL_VAR name=title>).
page = (<TMPL_VAR name=page>).
------------------
<TMPL_PROCESS \$page>
------------------
<TMPL_GET page3.eval>
";
close $fh;
}
if (open my $fh2, ">", $file2) {
print $fh2 "I am file2 ($file1).
title = (<TMPL_VAR name=title>).
";
close $fh2;
}
my $ht = Template::Alloy->new(filename => $file1, absolute => 1);
$ht->param(title => 'The title');
$ht->param(page => $file2);
$ht->param(page3 => sub { "This is a dynamically included string.\ntit
+le = (<TMPL_VAR name=title>)\n"});
print $ht->output;
__END__
prints
I am file1 (/tmp/filessyE4j).
title = (The title).
page = (/tmp/filez6O6TO).
------------------
I am file2 (/tmp/filessyE4j).
title = (The title).
------------------
This is a dynamically included string.
title = (The title)
my @a=qw(random brilliant braindead); print $a[rand(@a)];
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.