Loops
If we're going to get anywhere with our RSS example, we'll need to loop over a series of items--the stories in our newsreel. Thankfully, HTML::Template provides the <TMPL_LOOP> pseudotag for treating a variable as an array. For instance, the following code:
<ul> <TMPL_LOOP NAME=STORIES> <li> From <TMPL_VAR NAME=FEED_NAME>: <TMPL_VAR NAME=STORY_NAME> </li> </TMPL_LOOP> </ul>
when provided the appropriate data structure, loops over the items in the STORIES array reference and produces output like so:
<ul>
<li>
From Slashdot: NASA Finds Monkeys on Mars </li>
<li>
From use.perl: Perl 6 Release Predicted for 2013 </li>
</ul>
The trick is that the array reference needs to contain an array of hashes, and each hash provides the appropriate variable names:
$template->param(STORIES => [ { FEED_NAME => "Slashdot", STORY_NAME => "NASA Finds Monkeys on Mars" }, { FEED_NAME => "use.perl", STORY_NAME => "Perl 6 Release Predicted for 2013" } ]);
Please enable JavaScript to view the comments powered by Disqus. blog comments powered by