use strict; use warnings; use HTML::Template; my $str = <". STR my @db = ('', qw(the quick brow fox jumps over the lazy dog should suffice)); my $template = HTML::Template->new (scalarref => \$str); my %usedParams = map {lc ($_) => 1} $template->param (); my @usedIds = map {m/section_(\d+)/; $1} keys %usedParams; $template->param ("section_$_" => $db[$_]) for grep {defined $_ && length $db[$_]} @usedIds; print $template->output (); #### For example, if user defines in a template a string "%section_10%" I retrieve value "10", make a mySQL query to select some text from row with id = 10 and then replace text with query result "should".