tags:

views:

118

answers:

3

Hi, I want to write a header and footer perl scrip that will return the headers and footers of my individual webpages. I was assuming I would do some sort of include of that file in the part of the html file I want it to appear. Is that the way I should go about this, if so how do I do that? If anyone has any other suggestions for better ways to do this, I'd be very interested. Thanks.

+3  A: 

Have a look at Template Toolkit. It allows you to insert server side code, that is evaluated before returning the page, so you can easily add header/footer code to pages this way. The toolkit is easy to use and additional features can be implemented in Perl.

Brian Rasmussen
Please leave a comment when down voting.
Brian Rasmussen
At a guess I'd say it's because it's just a link, with no actual contribution from *you*. What is the OP looking for with it? Why is it a good solution? What does it do? How should the OP use it? I've downvoted a fair number of link-only answers on SO, but not this one. Just guessing. :)
jalf
@jalf: It could be. However, I do find link only answers to be useful. If you don't know a given tool a simple pointer in the right direction may be all you need. I would not down vote such an answer, but I'll keep it in mind then - and add a tiny bit of context to make you happy :)
Brian Rasmussen
True, normally I'd just leave a link-only answer alone, rather than downvoting it. Still, I'm guessing this is why it's downvoted
jalf
I appreciate your input.
Brian Rasmussen
A: 

Subclass CGI.pm and add custom methods. For example:

MyApp.pm

package MyCGI;

use warnings;
use strict;
use base qw( CGI );

sub page_header {
    my $self = shift;
    return $self->div( { 'id' => 'header' },
        $self->h1('Welcome to my home page') );
}

sub page_footer {
    my $self = shift;
    return $self->div( { 'id' => 'footer' },
        $self->tt('Copyright © 2010. All rights reserved.') );
}

sub content {
    my ( $self, $paragraph ) = @_;
    return $self->div( { 'id' => 'content' }, $self->p($paragraph) );
}

1;

page.pl

#!/usr/bin/env perl

use warnings;
use strict;
use MyCGI;

my $page = MyCGI->new();
print
    $page->header(),
    $page->start_html('My home page'),
    $page->page_header(),
    $page->content('My own content'),
    $page->page_footer(),
    $page->end_html();

Then, you can use MyCGI whenever you want the custom/subclassed methods.

Alan Haggai Alavi
and essentially have no html files whatsoever?
Silmaril89
-1: Very inventive, but using CGI.pm's HTML-generation functions is just another way of mixing code with presentation and leads to all the maintainability problems that brings. Use a proper templating solution, such as Template::Toolkit (as suggested by Brian Rasmussen), if you're going to go for fully-Perl-served pages, so that you can keep the presentation independent of the code providing its data.
Dave Sherohman
+3  A: 

If your server supports it, check out server side includes. They don't necessarily have to be written in Perl.

If not, and your headers and footers are simple, consider writing them in Javascript. It is easy to include a Javascript script in your HTML code that will make a bunch of document.write calls and you'll be done.

If not, and your headers and footers are not simple, and if you don't know Javascript and if you might be a masochist, still consider using Javascript.

There are probably some other newer technologies to accomplish this that the young whippersnappers here can tell you about, but for me server-side includes and Javascript ain't broke.

mobrule
thanks, that was very helpful
Silmaril89
As far as including content is concerned, JavaScript is broken. Having your navigation vanish because JS isn't around is very bad for (some) users and very bad for your position in search engines.
David Dorward
Yea, I am planning to use server side includes to include my perl cgi scripts. Thanks for the comment though.
Silmaril89