Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Perl Monk, Perl Meditation
 
PerlMonks  

html template in a perl script

by archen (Pilgrim)
on Sep 21, 2003 at 01:41 UTC ( #292942=perlquestion: print w/ replies, xml ) Need Help??
archen has asked for the wisdom of the Perl Monks concerning the following question:

First a bit of background. I started doing HTML for about 4 years before I ever touched perl, and I consider myself quite good at it. When I started creating Perl scripts I quickly started seeing a problem with generating html. Bad enough that you start doing here-documents, but then people suggest that you start generating html with CGI. As a writer of html, I consider CGI html to be a total abomination - it's not html, and it's pretty ugly when you start doing anything more than the basics. Then one day I found html::template - the perfect way to abstract html and Perl. Then today I realized that I could in fact include html and perl in the same document AND keep them separate at the same time. Just attach html::template to a data block:
---------------------------------------------------
#!/usr/local/bin/perl/ -wT use strict; use HTML::Template my $template = HTML::Template->new( 'filehandle' => *DATA ); #-- blah blah --# __DATA__ html template stuff
---------------------------------------------------
I've tried it and it works, but my question is, is this a good idea? I was wondering if anyone had any comments on how this would affect performance or if there are any other considerations. Currently I can find a lot of stand alone scripts that would benefit from this, especially since I often write scripts for a friend and he sometimes sets the script to chmod 660 and the template to 755. Generally there is no voodo going on at my server (no mod_perl and such), just strait CGI(::Simple).

Comment on html template in a perl script
Download Code
Re: html template in a perl script
by cees (Curate) on Sep 21, 2003 at 02:06 UTC

    Sounds like a reasonable way to organize things to me, as long as your CGI scripts are quite simple. I wouldn't use that model myself, because I use lots of includes in my templates to provide a consistent look and feel to the application. But if your scripts only needs one template, then the DATA method seems like a good option...

    It will also be slightly quicker than an external template, since perl will only need to access one file, instead of opening two.

      cees , can you provide anymore info on the DATA method? I use HTML::Template, but sometimes it's overkill. It sounds like this might be simpler/faster/easier.

      Thanks.
        Um, the DATA method is what we are talking about! You just put your template in the DATA section of your Perl script. I use this all the time for simple CGI scripts and examples i post here at the Monastery - but for anything serious, i use another file for the template. As for your overkill problem, the more you use HTML::Template the less the overkill seems to be a problem.

        As for your problem at Re: html template in a perl script, have you looked into Template yet? Kake Pugh has an excellent article over at perl.com: How to Avoid Writing Code that demonstrates how well that Class::DBI works with Template Toolkit.

        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)
        

        If you read the original question again, you will see what the DATA method that I am talking about refers to. It is a method for adding a __DATA__ section to your perl code, where perl will automatically provide this data in the file handle DATA. The above question gives an example of how to use this using HTML::Template.

        For more info, check out perldoc SelfLoader for some more info on __DATA__.

        - Cees

      Template includes in a template is something I hadn't thought to consider. I usually don't include other templates, but I can see how it could become an orginizational nightmare having some HTML in the scripts, and some in templates laying around elsewhere. Good call, I'll keep this in mind!
Re: html template in a perl script
by graff (Chancellor) on Sep 21, 2003 at 02:27 UTC
    Hey, if it works for you, why not?

    I would guess that most people who use HTML::Template do it because they have other people who are not perl programmers but need to work directly on the HTML layout -- no one (least of all the non-programmers) would want non-programmers altering contents of perl scripts just to change the appearance, layout or "static" information of a web page. Or maybe programmers have one text editor mode for perl and a different one for HTML, and it's just easier to do these two jobs in separate text files.

    If those situations don't apply to you (you alone are controlling the static layout as well as the dynamic content and you handle HTML editing the way you handle perl editing), then this seemingly contradictory use of HTML::Template is simply a way of organizing your own code to be neater, more compact and easier to maintain -- and easier to adapt later on, if at some point you decide to use HTML::Template in the more usual manner, to keep the code and the layout completely separate.

Re: html template in a perl script
by jdtoronto (Prior) on Sep 21, 2003 at 03:26 UTC
    Well, as graff says, if ti works for you why not. Sure CGI generated HTML isn't so clever, not nowadays anyway. But of course CGI.pm has been around for a long time!!

    The idea of using HTML::Template to separate the Perl and the HTML makes sense. Programmers work on the Perl, designers work on the HTML. If you are the only one working on what are Perl scripts with a 1:1 Perl/HTML doc relationship then fine.

    Although I have used this trick for very simple scripts I find it a dangerous concept for general purpose use at it 're-integrates' the Perl and HTML, albeit in a more maintainable form than using CGI.pm or just using HERE documents.

    jdtoronto

      Although I have used this trick for very simple scripts I find it a dangerous concept for general purpose use at it 're-integrates' the Perl and HTML, albeit in a more maintainable form than using CGI.pm or just using HERE documents.

      What does that mean ? I dont know how separating code and html will be dangerous.

      I have successfully used templates for my smaller scripts. There has been times when I had to dump a concise text report or a long html report. I could do that by just switching the templates, without the core part of the script having to bother what kind of report we are going to generate. I thought templates are a boon when doing that. There is also an article in The Perl Review Separating Code, Presentation, and Configuration that deals with this in detail.

      -T

        I can't speak for jdtoronto, but i am pretty sure that he was talking about keeping the HTML and Perl seperate, but in the same text file - i.e., put the template in the DATA section. I agree that this is nice for small quickies, but for anything serious, you should go ahead a seperate the code and template into two (or more) text files.

        So then the question is (again) - why not put the code and template in the same text file? Because you will most likely end up having to seperate them in the long run when scalability rears it's ugly head. (please see 3Re: HTML::Template - complex sites for a technique for dealing with a more complex template structure.)

        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)
        
        Well, it is dangerous in the sense that it is only, IMHO, a partial separation of Perl and HTML. As a pragmatic point I consider that the programme and the presentation should 'always be separated when it is reasonably possible to do so'.

        The viewpoint I was hoping to express here is that if this technique works for you, then use it. But beware, it is only a half measure and wilst I would consider it appropriate for small or simple applications. I think the original question included a remark about the writer preparing some scripts for a friend where a simple script and a single template were involved. As a method of packaging the script and template where the author is likely to be the one modifying both parts then it might be a pragmatically acceptable alternative.

        jdtoronto

Re: html template in a perl script
by bradcathey (Prior) on Sep 21, 2003 at 13:31 UTC
    My web customers, some who are HTML savvy, were asking for the ability to edit their HTML, which I had previously buried in CGI scripts. For precisely the reason mentioned previously, I switched to H::T. If I'm careful, I use H::T to simply fill in data from the MySQL database, but if I'm not careful, I find myself using H::T to actually write custom HTML, with the data embedded, to the template, thus, defeating the purpose.

    The challenge for me, then, is to write more clever HTML or CGI to keep H::T doing what it's supposed to do.

    I find no other downsides to using H::T, at least in terms of performance. Although like using any module, I feel a little guilty that it's so easy to use someone else's hard wrought code.

    All in all, H::T is a winner for me.

Re: html template in a perl script
by samtregar (Abbot) on Sep 21, 2003 at 16:59 UTC
    I think it's a fine idea, as long as you're reasonably sure you're the only one that will ever need to edit the HTML. In fact, this is pretty much exactly what I did in HTML::PopupTreeSelect. I used a here-doc rather than DATA but the effect is the same.

    -sam

    PS: Ignore anyone that suggests you switch to another templating system. HTML::Template has always served me well and I expect it will continue to do the same for you!

Re: html template in a perl script
by CountZero (Chancellor) on Sep 21, 2003 at 21:40 UTC

    Fine for simple matters, but what would you do when you need to have access to two or more templates from the same script?

    CountZero

    "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      In that case you use Inline::Files.
Re: html template in a perl script
by dont_you (Hermit) on Sep 22, 2003 at 00:25 UTC
    I work daily with HTML templates and CGI, and founded that...

    - If you keep the template in a separated file, you can drop the template into a browser and look how it looks like.

    - If you keep the template in a separated file, you can edit it with an WYSIWYG editor.

    - In a CGI enviroment, avoiding to open an extra file will bring a very small performance gain, and very very small if the extra file is already in the OS file cache. Normally, there are some more expensive tasks: forking the process, loading modules, connecting to DB (if any), parsing template, doing some real process...
    If you are worried about CGI performance, I would suggest to take a look at SpeedyCGI: mod_perl comparable performance (slower, obvious) under CGI.

    Josť

Re: html template in a perl script
by benrwebb (Scribe) on Sep 23, 2003 at 20:01 UTC
    I'm sure what you are doing will work great for you, but I'm wondering how you are managing with only one template per page? I know that many have said this so far, but let me give you a more detailed account of why we are all questioning your use of only one template.

    On any given page I actually load at least 4 templates. One for the top, one for the bottom, one for the left side menu, and one for the main part of the application. If I have an administrative block (for instance, the posting portion of a news page) then a fifth may load for that(depending, of course, on the currently logged in user). The only way I could see managing my site with a template in a DATA section would be to include the top, bottom and left menu templates, and then put the actual "application" info into the data section. I would genuinely consider doing that (it is a novel idea and I rather like it) but for two things: 1- I totally switch my display for errors(I have a template that gets called in place of everything else for error reporting) and 2- I have a team working on my site so I'm afraid that it might make CVS even weirder than it already is.

    Good idea though, if you can pull it off.

      I wasn't planning on making one template for EVERY script, but it does come in handy in many instnaces. For instance I have a lot of utility scripts for editing Berkeley Databases and various other things. These scripts are created as a drop in solution and can often end up just about anywhere. The HTML for these scripts never really changes. All a template really does is adds another thing to cart around with the script.

      If you're creating a dynamic page where multiple pages need to retain the same look, then sharing similar templates makes sense. I do what you do quite often with multiple parts, but usually I use server side includes to call perl content. I'm pretty picky on performance, so I often find it's better to have cron run scripts to generate "static" content, or just have a perl script generate "static" content for each change. You can slightly improve security by this method by moving scripts out of harms (hackers) way.

      I've never been the sort of guy to make an entire website have a single look, mainly just by section so often I end up designing many more templates. Cutting these pages apart gives you flexibility, but having four template parts gets really messy when you're talking going six html tables deep and spanning multiple template files.

      This is often more of an issue with an associate of mine who isn't so CGI oriented. He has a bad habbit of seeing an html page (template) and linking that instead of the CGI script and only later looks at it and sees there's no conetent!

Log In?
Username:
Password:

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

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

    April first is:







    Results (433 votes), past polls