Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Over the past couple of months, I've become acutely aware of the value of separating presentation logic from application logic. A project I am currently working on in that other web scripting language (you know, the one with Poor Hash Practices ;-) has no such separation, and becomes less maintainable and more of a jumbled mess of code with each new page. I find myself turning to the Template Toolkit for more and more of my dynamic generation needs.

Lately, though, I've been having a dilemma as to the style which my templates take. Anyone who spends any time in the Monastery knows that CGI.pm is one of our oldest and most sacred incantations. So sacred, in fact, that seldom does a hand go unslapped for statements like: print "<H1 STYLE=\"color: blue\">Hi, $attrib->{name}</H1>" ; that could be replaced with: print $q->h1( { -style => 'color: blue' }, 'Hi,', $attrib->{name} ) ;

The pros of using the latter method are evident on so many levels. First, the code is easier on the eyes. Personally, I cringe at having to escape quotes -- readability takes a sharp nosedive when this happens. Second, Perl will actually prevent you from generating broken HTML, and let you know when you try to. IMO, and the O of many, many others, It's just simply the way to go.

My question, in a nutshell, is this: how strongly do people feel about the use of CGI from within Template Toolkit?

My first Template Toolkit scripts used the CGI plugin. I found that, in some cases, it seemed to be serious overkill, especially for static elements. For example, a simple table with a page title and an image using CGI:

[% mycgi.table( { border => '0' }, Tr( td( [ h1( 'My Page' ), img( { src => 'mypage.png' } ) ] ) ) ) %]
And without:
<table border="0"> <tr> <td> <h1>My Page</h1> </td> <td> <img src="mypage.png" /> </td> </tr> </table>

Is it just me, or is the latter example easier to visualize?

Another advantage to the second method is that I don't need to generate something from a template to see how it looks -- I can simply load my template in a web browser. This doesn't give you the full effect of your template (eg. FOREACH loops), but overall I find it extremely helpful. When you use the CGI plugin, it seems to me that you lose some of this ability.

I'd really appreciate getting some feedback about the methodology people use when using Template Toolkit on small-to-medium-scale web development projects. Does the edict of use CGI carry over just as strongly to templates, or do most people use handwritten HTML instead? Is it considered poor taste to mix HTML with CGI plugin code?

As a small aside, I'm sure the HTML::Template camp is laughing at this, as they never have to deal with it.

Much thanks for your boundless knowledge and wisdom.


_______________
D a m n D i r t y A p e
Home Node | Email

In reply to TT2 -- A Matter of Style by DamnDirtyApe

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (4)
    As of 2019-05-21 02:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you enjoy 3D movies?



      Results (129 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!