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

HTML templates => no HTML in Perl code

by emilford (Friar)
on Apr 22, 2004 at 03:20 UTC ( [id://347203]=perlquestion: print w/replies, xml ) Need Help??

emilford has asked for the wisdom of the Perl Monks concerning the following question:

I am using the template example in the Perl Cookbook to dynamically generate pages. I have looked into Template::Toolkit, but I'm uncertain whether or not it will be available on the server my Perl code is to run on.

The template example in the Perl Cookbook should be more than sufficient. My dilemma, however, is determining the best way to use it. I am going along with using %%ABC%% placeholders to swap out things generated by my Perl code. My goal is to keep ALL HTML code out of my Perl script so that any design modifications can be done via the external template files.

My current setup is to have two template files for a single page. The first template page contains all of the HTML code to create the bulk of the page and two placeholders, %%PAGETITLE%% and %%MAIN%%. %%MAIN%% is the placeholder for the page content.

[html] [body] [table] [tr] [td] [table] %%MAIN%% [/table] [/td] [/tr] [/table] [/body] [/html]
The second template file would contain how the contents of that page should be formatted. This should only consist of [tr] and [td]tags.

[tr] [td]%%VAL1%%[/td] [td]%%VAL2%%[/td] [td]%%VAL3%%[/td] [/tr]
So, my Perl code would fill in the placeholders of the second template file. Grab that, and stick it in the first template file as the %%MAIN%% placeholder.

Hopefully this makes sense. Is this a good approach? Any better suggestions for keeping all HTML code out of my Perl script?

Replies are listed 'Best First'.
Re: HTML templates => no HTML in Perl code
by helphand (Pilgrim) on Apr 22, 2004 at 03:36 UTC

    You might take a look at HTML::Template as an alternative. It is fairly lightweight and I don't think it requires anything beyond the pm file so you should be able to install in the same location as your cgi script. (create a HTML subdir then put the Template.pm file in it).

    Scott
Re: HTML templates => no HTML in Perl code
by kiat (Vicar) on Apr 22, 2004 at 04:08 UTC
    Hi emilford,

    I'm using HTML::Template and I'm absolutely happy with it. It can do what you what you intend to do with your placeholders, and more. You can have <TMPL_IF>, <TMPL_INCLUDE> and <TMPL_ELSE> to control the stuff to insert in a particular template.

    I used to freely mix perl code with html and the resultant code looks awful. Having said that, I must admit that I still have some html in my code because I find it more convenient to do so in some cases. Probably a bad practice but way better than my old style...

Re: HTML templates => no HTML in Perl code
by Tomte (Priest) on Apr 22, 2004 at 09:30 UTC

    I concur with the answers you already got from helphand and kiat; and I like to stress one point: You need HTML::Template or something like this sooner than you now know!

    It is not feasible to have a template for every single case of "it should look so and so"; simple conditionals and loops are essential to be able to really seperate code from look

    Though I prefer Template::Toolkit, HTML::Template is more than adequate and, as said before easy to install.

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-16 10:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found