What i am hearing is that you have N numbers of thingies you want display in one place 5 times, and in another the full N. Well, sounds like you need a loop, and i don't see a single <tmpl_loop> tag in your template. That's a big problem, because that tag is precisely what is needed to make this work. Here's how i would handle it (warning: theorectical ideas ahead):

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:

$VAR1 = [ { title => 'foo', article => 'bar', }, { title => 'baz', article => 'qux', }, # etc. ];
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!

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>

This line probably deserves some elaboration:
articles => ('home' eq $page) ? [(@$articles)[0..1]] : $articles,
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.

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)

In reply to (jeffa) Re: HTML::Template 'splicing' help by jeffa
in thread HTML::Template 'splicing' help by hacker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.