Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Multiple template coordination using HTML::Template

by ryan (Pilgrim)
on Dec 29, 2001 at 15:49 UTC ( #135107=perlquestion: print w/replies, xml ) Need Help??

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

I've recently discovered HTML::Template and am very happy with what it can do for me. In an effort to learn more I am modifying several previous CGI scripts I made when I didn't know about templating.

One of these scripts is a single CGI that has about 10 'faces', depending on the POST data it displays a totally different interface, such as Admin, File List, Reports etc... The CGI could be split up, but I prefer it in one neat file .. I suppose this is another topic, is cramming heaps into the one file frowned upon? I realise this is a bit of a pandora's box :) It is basically a small file management system in a single file.

I was after some guidance as to the best way to handle this using templates. I so far have thought of making a separate template file for each 'face' and putting a simple conditional IF block in the beginning of a 'generic' HTML template that then imports the specific template required for that face. This method would allow me to still maintain a single CGI file, but keep the multiple 'faces' of the CGI, while still having good code/design separation.

Would this be considered workable? Any thoughts or accepted standards in this area?

TIA .. Ryan
  • Comment on Multiple template coordination using HTML::Template

Replies are listed 'Best First'.
Re: Multiple template coordination using HTML::Template
by rob_au (Abbot) on Dec 29, 2001 at 16:17 UTC
    There are a couple of issues here, primarily alternating between application design and display formatting.

    With regard to your direct question regarding HTML templates, there is absolutely no reason why multiple templates cannot be defined and called depending upon the "state" of your CGI application. If you are trying to incorporate all of your application output within a single template with multiple TMPL_IF and TMP_ELSE (or even more succinctly using HTML::Template::Expr), you will rapidly find your available display options either at best greatly convoluted or at worst severely curtailed.

    The more general issue which you may want to consider is your application design. While there is nothing wrong with incorporating all aspects of your CGI interface into a single script, the question that is raised in my mind is as to how you are incorporating these interfaces together - Is your application littered with if-elsif-else conditionals? In which case, more powerful stateful CGI frameworks exist which will minimise your development workload enormously - In particular, you may wish to have a look at CGI::Application which intergrates exceptionally well with CGI and HTML::Template, shortening the learning curve somewhat.


    perl -e 's&&[@.]/&&s&.com.&_&&&print'

Re: Multiple template coordination using HTML::Template
by Hero Zzyzzx (Curate) on Dec 30, 2001 at 03:11 UTC

    You should look into CGI::Application, which tightly integrates HTML::Template into its core. Basically, with CGI::Application, each of your "faces" would be a runmode, and then your application has a "setup" method that helps define the flow of your application. Looking at the "setup" subroutine of a app built with CGI::Application, you can usually figure out what each "face" or runmode does.

    The other advantage is the CGI::Application is a gentle introduction to OOP. You can easily create a "superclass" that holds methods that you share among different modules of your application- you might split your report functions from your file list functions, but you can then share methods among them simply via the superclass.

    There are a lot of advantages to using this module- it forces you to code cleanly, you can easily change the functionality and templates used for an entire application without recoding, and it's mod_perl friendly, should your application ever reach that level.

    Check it out!

    -Any sufficiently advanced technology is
    indistinguishable from doubletalk.

      Thanks for the guidance monks. I have just had a quick look at the concept of CGI::Application and it certainly looks like a winner for my purposes. Anything that forces me to design things properly is good in my book. Of course code-reuse remains at the will of my power to program it as such, but the potential is there. The program is already running under mod_perl which then made me look at Apache::Session which may have some advantages for me also.

      All in a day's wide-eyed learning ... Ryan
(jeffa) Re: Multiple template coordination using HTML::Template
by jeffa (Bishop) on Dec 29, 2001 at 22:14 UTC
    I would strongly consider looking into Style Sheets for this kind of problem:
    # sample template <html> <head> <link rel="StyleSheet" href="<tmpl_var name=css>" type="text/css"> </head> <body> <!-- stuff --> </body> </html>
    Now all you need to do in your CGI script is figure which style sheet to use.


    (the triplet paradiddle)
Re: Multiple template coordination using HTML::Template
by dorko (Prior) on Dec 29, 2001 at 19:33 UTC

    Given that you want to keep all the CGI code in a single file, what you propose would work just fine. (Eventually you might want to change that approach (or not), but for now you've got your reasons for wanting it that way, and that's good enough for me. ;)

    My only thought might be to move the conditional IF block to the CGI rather than having it in the template. Just a thought.

    Either way will get the job done.



Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2022-01-17 04:10 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (51 votes). Check out past polls.