Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

HTML::Template __even__

by Anonymous Monk
on Jun 04, 2014 at 15:40 UTC ( [id://1088667]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

I am trying to get alternate color rows using HTML::Template, but I can not understand why this is not working, the "__even__" is not been evaluated I think.
Here is how I am trying it, any suggestions?
<style type="text/css"> <TMPL_IF NAME="__odd__"> .row_color { background-color: green }; </TMPL_IF> <TMPL_IF NAME="__even__"> .row_color { background-color: yellow }; </TMPL_IF> </style>
... <TMPL_LOOP NAME=DATA> <tr> <td class="row_color"> Row <TMPL_VAR NAME=__counter__></td> </tr> </TMPL_LOOP> ...

Replies are listed 'Best First'.
Re: HTML::Template __even__
by taint (Chaplain) on Jun 04, 2014 at 16:24 UTC
    Greetings, AM.

    FWIW, if you use a CSS file. You could simply add

    /* for ODD row alternation */ tr:nth-child(odd) {background:#f6f6f6} /* for EVEN alternation */ tr:nth-child(even) {background:#f6f6f6}

    Best wishes.

    --Chris

    ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

      You actually have to specify background-color or at least background for that to work.

        LOL. Right you are Mr. Muskrat. But I only posted enough to infer it's intended usage.

        One might also add/choose other attributes

        border-bottom:0.2em solid #d00;
        or
        display:none;
        ;)

        Thanks for mentioning it Mr. Muskrat. Updated, for better clearity. :)

        --Chris

        ¡λɐp ʇɑəɹ⅁ ɐ əʌɐɥ puɐ ʻꜱdləɥ ꜱᴉɥʇ ədoH

Re: HTML::Template __even__
by InfiniteSilence (Curate) on Jun 04, 2014 at 16:12 UTC

    Read the pod. Search for loop_context_vars.

    Celebrate Intellectual Diversity

Re: HTML::Template __even__
by RonW (Parson) on Jun 04, 2014 at 16:15 UTC

    In your CSS, you should define 2 row color styles, not 2 versions of the same style:

    .row_color__odd__ { background-color: green }; .row_color__even__ { background-color: yellow };

    Then change your HTML template to to use the corresponding style for each row.

      That would not work since it is inside of a loop. Can you post some code to show what you are saying? Thanks!
        RonW is correct - what you are trying will not work. You would be better setting the class in Perl:
        my $rows; my $class = 'odd'; for my $name ( qw(Odd1 Even1 Odd2 Even2) ) { my $hash = { name => $name, class => 'row_color_'.$class, }; push(@$rows,$hash); $class = ( $class eq 'even' ? 'odd' : 'even'); } $template->param(DATA => $rows);
        Then in your template:
        <TMPL_LOOP NAME=DATA> <tr> <td class="<TMPL_VAR NAME="class">"> Row <TMPL_VAR NAME="name"></td> </tr> </TMPL_LOOP>
        Output:
        <tr> <td class="row_color_odd"> Row Odd1</td> </tr> <tr> <td class="row_color_even"> Row Even1</td> </tr> <tr> <td class="row_color_odd"> Row Odd2</td> </tr> <tr> <td class="row_color_even"> Row Even2</td> </tr>

        Sure it would. You'll need two different classes, and then inside the loop select one of them using the TMPL_IF __even__ and __odd__ template constructs.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1088667]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-03-29 15:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found