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

Afternoon monks,

Im writting a CGI script and I would like to be able to use it on differant sites and have it fit their design (without having to rewrite it all the time). The script is majorly tied into the page like how perl monks is. I have no clue how to do this. but if anyone can help, thanks

regards, jasper

Replies are listed 'Best First'.
Re: CGI , HTML
by hardburn (Abbot) on Oct 24, 2003 at 20:27 UTC

    Use HTML::Template or another templating system. Preferablly, the sites themselves would be done in a templating system and all the various CGIs do is fill in the data.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    :(){ :|:&};:

    Note: All code is untested, unless otherwise stated

Re: CGI , HTML
by synistar (Pilgrim) on Oct 24, 2003 at 20:52 UTC

    You should see this article by perrin:

    Choosing a Templating System

    It goes over most of your options for a templating system (I also recommend HTML::Template for its simplicity).

    Another method would be designing the site with a 100% CSS layout. This would allow you to "reskin" the sites simply by loading different stylesheets. Here is an example of that method. This is my preferred method but requires pretty good knowledge of CSS.

Re: CGI , HTML
by Anonymous Monk on Oct 24, 2003 at 21:14 UTC
    Great, thanks. I'll give that a look through and try out later when I'm not at work. Writting a shopping cart, and I'm quickly finding myself not knowing what to do next. So hopefully this template module will take care of the html stuff, and let me focus more on the cgi side of it.
Re: CGI , HTML
by jacques (Priest) on Oct 24, 2003 at 20:56 UTC
    Im writting a CGI script and I would like to be able to use it on differant sites and have it fit their design (without having to rewrite it all the time).

    hardburn is right. You should separate the html from the program. There are many ways available to do this.

Re: CGI , HTML
by toonski (Beadle) on Oct 24, 2003 at 20:37 UTC
    Well, one thing you could do is load the page into a variable, and then regex yourself some special tags, like such:

    $htmlsource =~ s/<title>/$title/g; But if you're getting a lot of page hits, that's a *bad* thing. Another cheap option is to include the page source within its own '.cgi' file and "requre" it.

    #INSIDE HEADER.CGI print " <html> <head> <title>$title</title> </head> </body> "; } #INSIDE INDEX.CGI require "header.cgi";

    this is a bit faster but a bit more cumbersome to incorporate page design into. Then there's always HTML::Template, but I dont have the permissions on my server to try it out. Anyhoo, tha'ts alls I gots.

      Actually, if you have permissions to put any perl script up on your web site, then you have permissions to put up Modules, just not in the "normal" place.

      Try this:
      * make a dir called myModules
      * make a dir called HTML inside it
      * copy Template.pm into it from CPAN (or do it the proper way and have the installer install into your myModules dir)
      * In your script do:

      # use lib "myModules"; #this example uses a relative path use HTML::Template; # do real stuff here

      -------------------------------------
      Nothing is too wonderful to be true
      -- Michael Faraday