在 html 中包含 perl cgi 脚本

发布于 2024-08-23 06:31:31 字数 126 浏览 4 评论 0原文

我想编写一个页眉和页脚 Perl 脚本,它将返回我个人网页的页眉和页脚。我假设我会将该文件包含在我希望它出现的 html 文件部分中。这是我应该采取的方式吗?如果是的话我该怎么做?如果有人对更好的方法有任何其他建议,我会非常感兴趣。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天冷不及心凉 2024-08-30 06:31:31

如果您的服务器支持,请查看服务器端包含。它们不一定必须用 Perl 编写。

如果没有,并且您的页眉和页脚很简单,请考虑用 Javascript 编写它们。在 HTML 代码中包含一个 Javascript 脚本很容易,该脚本将进行一系列 document.write 调用,然后您就完成了。

如果没有,并且您的页眉和页脚并不简单,并且如果您不懂 Javascript 并且您可能是一个受虐狂,那么仍然考虑使用 Javascript。

可能还有其他一些较新的技术可以实现这一点,这里的年轻自吹自擂者可以告诉你,但对我来说,服务器端包含和 Javascript 并没有被破坏。

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.

素染倾城色 2024-08-30 06:31:31

查看模板工具包。它允许您插入服务器端代码,该代码在返回页面之前进行评估,因此您可以通过这种方式轻松地将页眉/页脚代码添加到页面。该工具包易于使用,并且可以用 Perl 实现附加功能。

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.

墨离汐 2024-08-30 06:31:31

子类 CGI.pm 并添加自定义方法。例如:

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();

然后,只要您需要自定义/子类方法,就可以使用 MyCGI

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文