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

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

Replies are listed 'Best First'.
Re^3: Code and html separation - always or mostly doable?
by saberworks (Curate) on Jun 17, 2004 at 05:53 UTC
    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
        It's an interesting approach, and I like it better than mine, however, I still feel like it's forcing me to "mess with" display logic in the template. I have to start setting bgcolor hash entries which are nothing more than display logic in my data hash. Imagine if I wanted every third row italicized, bolded, or underlined. I'd have to do the same thing (pass it a class in my perl code). Thanks for your help and interesting solutions, I just wish there was a better way. A way in which I can pass the results say directly from fetchall_arrayref to the template and have the template worry about setting the colors.