Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-19 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found