Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Code and html separation - always or mostly doable?

by saberworks (Curate)
on Jun 16, 2004 at 18:50 UTC ( [id://367372]=note: print w/replies, xml ) Need Help??


in reply to Code and html separation - always or mostly doable?

I've always wondered how you separate things like alternating table row background colors. Currently, I assign it in code, but that sucks. HTML::Template doesn't appear to have enough logic features to allow you to do modulus division on each iteration and pick a bgcolor from an array. So I have color definitions in my code - which sucks if the designer wants to change the colors. Of course, then I could use something like a config file or store the colors in a db, but that way, it's separated into 3 different areas instead of just two.
my @colors = ('#FFFFFF', '#F0F0F0'); my $cnt = 1; foreach my $row (@$list) { $row->{bgcolor} = $colors[$cnt % 2]; # Assign $row to your template engine or AoH or whatever... $cnt++; }

Replies are listed 'Best First'.
Re^2: Code and html separation - always or mostly doable?
by amw1 (Friar) on Jun 16, 2004 at 19:45 UTC
    for Template Toolkit this macro will do it This does it using CSS classes for coloring but setting it in a different way works as well.
    create a block macro [% MACRO alt_color(loop_count) BLOCK %] [% IF loop_count % 2 == 0 %] color1 [% ELSE %] color2 [% END %] [% END %] in your template you do loop.count is a special property of the iterator [% FOREACH thing = things %] <td class=[% alt_color(loop.count) %]>[% thing %]</td> [% END %]
    for H::T you can do
    <tmpl_loop name='things'> <td class=<TMPL_IF __ODD__>color<TMPL_ELSE>color2</TMPL_IF>> thing</ +td> </tmpl_loop> you need to make sure you have loop_context_vars set to 1 in H::T to h +ave access to __ODD__/__EVEN__
Re^2: Code and html separation - always or mostly doable?
by Michalis (Pilgrim) on Jun 16, 2004 at 20:10 UTC
    Hi,

    If you only speak of two colors, it's quite easy in the template system I use (and I guess, even easier on standard Template systems).
    I create an array (call it @array) on my perl program, containing hashes. One of the hash elements is named "bgcolor" and is actually a boolean value. Let's say for example that it also has a "content" value which is string and a "cnt" value which is integer (a counter).

    The code notation provided works for HTMLTP, but it should work anywhere as the logical part.

    <table> <!--: ascending array :--> <tr class="<!--: if bgcolor :-->colored<!--: else :-->noncolored<!--: +end :-->"><td>[:cnt:]</td><td>The content of the [:cnt:]th element is + : [:content:]</td></tr> <!--: end :--> </table>

    The only remaining part is to define the "colored" and "noncolored" classes on your CSS.

    --
    Michalis
      Both of the above are fine for two colors, but in my code, you can easily add an arbitrary number of colors and it will still work. I think template systems should provide functionality to handle very common cases such as this. Maybe something like (haven't used html::template in forever so I don't remember the syntax for loops, but I'll guess):
      <TMPL_LOOP NAME=rows ALTERNATE=FFFFFF,F0F0F0,CCCCCC> <td bgcolor=rows.alternate> .... </td> </TMPL_LOOP>
      I guess I could add it ;)
        I thought I'ld let it as an exercise, but...
        If I use bgcolor as integer, not boolean, I can easily do that:
        <td class="color[:bgcolor:]">....</td>

        So I can not only define classes color1,color2 etc, but if in the future we decide we want a 57th special case we haven't thought of before, we don't need to change any code, we just create a class57 and "feed" the bgcolor with the number 57.
        As I said before, there is always a matter of good analysis in order to trully and forever seperate perl code (business logic) from presentation (I consider the loops and if/then/else cases in the final HTML pages to be presentation-specific and not business specific)
        --
        Michalis

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://367372]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-18 04:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found