Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

templates vs here docs

by grashoper (Monk)
on Dec 19, 2007 at 21:00 UTC ( #657977=perlquestion: print w/ replies, xml ) Need Help??
grashoper has asked for the wisdom of the Perl Monks concerning the following question:

I am fairly new to perl and was wondering if it would be better to use template toolkit or a templating system rather than a here doc? My site is currently using a here doc and xslt transforms for outputting html, its connecting to a sql database and pulling data from there, would it be better for me to move this to a templating system? System is running on windows and using IIS, Asp and activestate perl. The existing system is giving me trouble with how to do certain tasks, like add a navigational menu, since the content is being dynamically generated via xml files I am not sure how to create links into the pages etc. Am I missing something? I can't make the connection on how a templating system such as template toolkit will help with my project, currently it connects to sql database and pulls content out of that dependent on which account is being looked at, how would I utilize TT to do this more efficiently, there are several things I would like to accomplish that i am just stumped on how to pull off, like make links context sensitive for the help files,use seperate "branding" to be supplied by the accounts themselves. I wish there was more of a walkthrough on how to use this, the tutorial is not clear to me, the examples given are overly simple yet the explanation is lacking in detail, I can't even follow it are there any sites that go into more detail with examples you can try yourself etc. I want to use it I really do I just need some help to figure out how.

Comment on templates vs here docs
Re: templates vs here docs
by mwah (Hermit) on Dec 19, 2007 at 22:25 UTC
    ... using a here doc and xslt transforms for outputting html ...

    In the last months or so I wrote a bunch of Perl scripts for compute cluster control - which do lots of wicked rsh's into a cluster an present nice html output to the coworkers.

    I started out w/heredocs, then changed that to qq{..}-strings, then throwed the towel and used (sound on) a nice little module called:

    HTML::Template

    This was a stunning experience, after (short) working through the details, I ended up with a HTML-File per script (same name as the script + .htm) and several very clean and easy readable Perl scripts.

    The proloque to the action would be identical across the scripts, in my case that's sth. like:

    ... $scriptname = ... # the scripts name (my $srv = $ENV{SERVER_NAME}) =~ s/\..+//; # where are we now my $srvloc = "/srv/web/$srv/html"; # assume directory # prepare HTML output my $template = HTML::Template->new(filename => "$srvloc/cluster/$sc +riptname.htm"); ...

    the corresponding html would be edited in Dreamweaver (or whatever you like) and look like:

    ... <td class="pid"> <TMPL_IF idproc> <a href="/cgi-bin/myprocess.pl/ii/<TMPL_VAR NAME="host">/<TMPL_VA +R NAME="pid">" title="[pid No.]"><TMPL_VAR NAME="pid">&nbsp; </a> </TMPL_IF> </td> ...

    The variables set in the perl program would show up in the html as

    <TMPL_VAR NAME="varname">

    and the perl program would set the variables like:

    ... $template->param( host => $host, cpuid => $cpuid, cpuidcolor => $cpuidtab{$cpuid} ); ... ... print "Content-type: text/html\n\n", $template->output;

    where he last line would send the html to the browser.

    So that's what I'd recommend. HTML::Template is ideal suited for this kind of CGI-script things.

    Regards

    mwa

Re: templates vs here docs
by eserte (Deacon) on Dec 19, 2007 at 23:08 UTC
    I would recommend here docs only for small sites with low complexity. An aesthetic problem of here docs is that it's not possible to use conditional blocks or loops without in the here doc, rather, you have to split the here doc into multiple parts.

    My recommendation for a templating system is Template-Toolkit. It's widely in use, has many (useful) features, is extensible and it can be used for all kind of templating problems, not just html templating.

Re: templates vs here docs
by pfaut (Priest) on Dec 19, 2007 at 23:37 UTC

    One big advantage you get using a templating system is separating the code from the HTML. This allows a programmer to work on the code and a web designer to work on the HTML. If you're working alone this isn't as big of an advantage but I've found it helps me concentrate better on one when I'm not staring at the other. It's a lot easier to understand both the code and the HTML when they're compact and flowing instead of intermixed.

    90% of every Perl application is already written.
    dragonchild
Re: templates vs here docs
by richardX (Pilgrim) on Dec 20, 2007 at 00:17 UTC
    I use TT2 (Template Toolkit) for creating dynamic web sites from SQL databases very quickly and easily. I like TT2 because I can retrieve a SELECT FROM...WHERE from the content database and then loop through the data, use a column as a flag to tell me how to process the data, and then output it to the HTML.

    The power of the template language makes for MVC like another poster mentioned, the seperation of content, data, and templates.

    Richard

    There are three types of people in this world, those that can count and those that cannot. Anon

Re: templates vs here docs
by bradcathey (Prior) on Dec 20, 2007 at 01:39 UTC

    I can't recommend a templating system enough. I wrote dynamic Web sites with HERE docs for a couple of years and then switched to HTML::Template. Keeping the server-side separate from the client-side is huge in my graphic design firm where I have people doing HTML, but don't know a lick of Perl.

    The next huge advance for me was switching to CGI::Application, a framework that makes working with dynamic Web sites and templating systems so much easier (lots of great plugins).


    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: templates vs here docs
by Fletch (Chancellor) on Dec 20, 2007 at 01:57 UTC

    Having just recently gone back and TT-ified a report generator (that in 20/20 hindsight probably should have started with a template :) I'd offer at least this advice:

    If you do use here-docs and/or qq{} try and make as much effort as possible not to interleave any computation or manipulation in with the printing. That way in n months when you do break down and bite the bullet (because after the xth request to add just one more thing . . .; oh, and we want a Blackberry friendly version of the same information so you need to make another copy aimed at a 2" wide screeen but the BB browser is dane bramaged enough to not understand sufficient CSS to do it that way) you don't have to spend a bunch of time extracting that logic and moving it elsewhere.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (12)
As of 2014-12-26 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls