开发者

Including perl cgi scripts in html

I want to write a header and footer perl scrip that will return the headers a开发者_如何学编程nd 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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜