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

TT2 -- A Matter of Style

by DamnDirtyApe (Curate)
on Jun 13, 2002 at 17:12 UTC ( #174275=perlquestion: print w/ replies, xml ) Need Help??
DamnDirtyApe has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on TT2 -- A Matter of Style
Select or Download Code
Re: TT2 -- A Matter of Style
by thpfft (Chaplain) on Jun 13, 2002 at 17:40 UTC

    I don't see any reason to put CGI.pm calls in a tt2 template. You lose one of the biggest advantages of the toolkit: that html people can read and maintain the templates. And, you know, html is good at laying out pages. why would you do it in perl?

    i've always thought the html output functions of CGI.pm a rather odd half measure, but it's one which you no longer need: you're achieving a much more effective separation of delivery and display by other means.

    updated: rambled some more

      ...I agree here. I've always been reluctant to encourage students too strongly to use the output functions in CGI.pm because some of them will have to maintain their code. And I couldn't find any great rationale for the extra effort that would be involved in writing code to essentially spit out HTML that you'd have to form anyway in order to get the function to produce it.

      There are rare occasions in which it is useful to spit out a small section of HTML from the module functions, and I hadn't considered the very valid point in the original posting here that the module does allow poorly formed HTML to be noticed more easily.

      But as a general rule, there's usually a better way to embed the HTML in your code. And of course if you're working on anything that transcends the trivial, you'll want a template. Another post here addresses that nicely.

      ---v

(jeffa) Re: TT2 -- A Matter of Style
by jeffa (Chancellor) on Jun 13, 2002 at 18:09 UTC
    Actually, there is at least one good reason to use CGI.pm within TT2 or HTML::Template - generating form elements. That's the only reason i have found. :)

    Check out the discussion at Re: TMPL_LOOP within a TMPL_LOOP for more.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Yes. I generally generate all HTML with the exception of my form elements in the templates.

      Generating form elements based on dynamic information from a database is much more straight-forward using CGI methods. Also the stickiness of form fields using CGI is often very handy when a user hasn't filled out all necessary fields or wants to modify their search criterion in a small way.

•Re: TT2 -- A Matter of Style
by merlyn (Sage) on Jun 13, 2002 at 18:12 UTC
    I'd abstract it even further.
    [% BLOCK name_with_image -%] <table border="0"> <tr> <td> <h1>[% name %]</h1> </td> <td> <img src="[% image %]" /> </td> </tr> </table> [%- END -%] ... [% INCLUDE name_with_image name = "My name" image = "mypage.png" -%]
    That way the business part of what you're representing doesn't get mixed up with the presentation part. And you can easily redesign your site. Use the power of the Templates! The Templates will be with you, always!

    -- Randal L. Schwartz, Perl hacker

Re: TT2 -- A Matter of Style
by gloryhack (Deacon) on Jun 14, 2002 at 05:32 UTC
    Take a look at CGI::FastTemplate. For the last several years, I use it more often than not.
Re: TT2 -- A Matter of Style
by gothic_mallard (Pilgrim) on Jun 14, 2002 at 15:38 UTC
    Just a quick point of "styles" - You say that it's better to use CGI.pm for printing the line with a style attribute. Really it's even better than that to create the basic tag with CGI.pm and leave the style to an external Cascading Style Sheet (CSS) file.
    That way you can truly seperate presentation from content and implementation.

    Never forget, just because this code is being dynamically generated / templated that you shouldn't use proper HTML "style".

    J.

      This will probably get castigated for being OT - but maybe this deep in a related thread it won't get noticed:)

      Has anyone an opinion or a reference as to why the powers that be opted to require the use of class="someclass" for css/css2/XML?

      It struck me in my recent work with CGI/XML::Simple (see, it's Related!:) that 'they' missed a trick here. Instead of:

      <html><head><style> .someclass { ... } </style></head><body> ... <div class="someclass">This text has some class!</div> ... </body></html>

      Wouldn't it have been simpler, cleaner etc. to allow:

      <html><head><style> someclass { ... } </style></head><body> ... <someclass>This text has some class!</someclass> ... </body></html>

      That is to say: Why not allow the browser to render the XML directly in the browser by attaching the css directly to the XML tagged data?


      I appreciate that this is a meta discussion.. and do not expect an "answer" as such - but if anyone knows or has an opinion I would be interested to hear it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (13)
As of 2014-04-17 10:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (444 votes), past polls