http://www.perlmonks.org?node_id=822977

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

Hi, I am trying to use Perl to design the foundation of my website. It has multiple pages. Each page looks the same, except for the content section. Thus, I have a template page and what I do currently is use PHP's include() function to include the header and footer onto each page.

What I want to do now is use mod_rewrite to redirect mydomain.com/path/to/file to mydomain.com/index.pl?page=path/to/file and then process that variable "page" using CGI and HTML::Template (and other modules if necessary). Is there a way to set my .tmpl file to include a file based on what the variable "page" holds? Should these included files by html files or pl files? Also, some of the content files might require Perl in them. How would I get this done?

Thank you!

Replies are listed 'Best First'.
Re: How to design website structure?
by CountZero (Bishop) on Feb 13, 2010 at 12:28 UTC
    Do not re-invent the wheel: go and have a look at Catalyst which is just the framework you will need.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Re: How to design website structure?
by Your Mother (Archbishop) on Feb 13, 2010 at 17:30 UTC

    Mason and Template will be more "familiar" than HTML::Template as they allow you to embed code, macros, and mini-language directives. In TT2 (Template) you could have a structure like so-

    [% PROCESS header.tt %] [% PROCESS ${variable_template_name_from_code}.tt %] [% PROCESS footer.tt %]

    -and then pass the middle template name in your code. There are many ways to do this sort of thing and you'll need to read the docs carefully but you can already see how easy it is to organize templates into reusable parts.

      Another option with TT is to use the WRAPPER setting.
      my $tt = Template->new(WRAPPER => 'wrapper.tt', ...); $tt->process('whatever.tt', ...);
      You then put your header/footer code into wrapper.tt and, wherever you want the body content to appear, insert [% content %]. If the header/footer are in separate files, as in YourMother's example, wrapper.tt would be:
      [% PROCESS header.tt %] [% content %] [% PROCESS footer.tt %]
Re: How to design website structure?
by pajout (Curate) on Feb 13, 2010 at 13:01 UTC
    Kitsune, there are many ways how to do it. For instance, if pages are completely static, I do not use Perl but I design some layout.xml and page1.xml, ..., pagelast.xml and build final html pages using XSLT machine, then I publish that static html pages. Or, if pages are dynamic, you can use mod_perl handler or CGI to launch your Perl code when somebody requests your page. Once your code is launched, you can compute dynamic values and use some template system for htmlization. HTML::Template is good choice, Petal is another.