in reply to HTML::Template 'splicing' help
Somehow you are going to have a datastructure that holds each article's body and it's title, as well as other possible 'attributes'. I'll assume 'title' and 'body' are plenty. You will need to somehow (great place for map, BTW) munge you orginal datastructure into this form:
A list of hashes. This is going to be fed to the <tmpl_loop> tag in your template. For more info on loops and corresponding datastructes, read up on HTML::Template or check out HTML::Template Tutorial. Onward!$VAR1 = [ { title => 'foo', article => 'bar', }, { title => 'baz', article => 'qux', }, # etc. ];
Now, at this point you have a data structure ready to be looped, but, the home page is different that the actual artice page. Whether you decided to have two complete different sets of code or one is up to you, my example below will use the later (one):
Run the following script first with no arguments to see what how the 'article' page renders, and then again with any argument to see how the 'home' page renders. The following code attempts to merely drive home some concepts, you will have to modify it to meet your real needs:
use strict; use HTML::Template; use Data::Dumper; # just a simple way to try both test cases my $page = (shift) ? 'home' : 'articles'; my $html = do { local $/; <DATA> }; my $template = HTML::Template->new(scalarref => \$html); my $articles = [ { title => q|Title 1|, body => q|blah blah blah, blah blah blah blah|, }, { title => q|Title 2|, body => q|blah blah blah, blah blah blah blah|, }, { title => q|Title 3|, body => q|blah blah blah, blah blah blah blah|, }, { title => q|Title 4|, body => q|blah blah blah, blah blah blah blah|, }, ]; $template->param( class => $page, articles => ('home' eq $page) ? [(@$articles)[0..1]] : $articles, ); print $template->output(); __DATA__ <table class="<tmpl_var name=class>"> <tmpl_loop name="articles"> <tr> <td><tmpl_var name="title"></td> <td><tmpl_var name="body"></td> </tr> </tmpl_loop>
If we are serving the 'home' page, then whe only grab the first 2 elements of @$articles and package them back in an array reference. You might also choose to truncate the values of each 'body' key from the elements. I chose not to here out of simplicity, and well, mostly laziness. Finally, the class="" attribute in the <table> tag can be used to format the resulting table differently.articles => ('home' eq $page) ? [(@$articles)[0..1]] : $articles,
Good luck, and feel free to ask me further questions.
jeffa
L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
F--F--F--F--F--F--F--F--
(the triplet paradiddle)
|
|---|