2007-04-08

【Perl】HTML::Template使ってみました。

Movable Typeでも使われているHTML::Templateを使ってみました。

以下のcpanの説明どおりにやったら意外にすんなりできました。(^^)v


例えば、 test.tmpl:


  <html>
<head><title>Test Template</title>
<body>
My Home Directory is <TMPL_VAR NAME=HOME>
<p>
My Path is set to <TMPL_VAR NAME=PATH>
</body>
</html>

そして小さな CGI プログラムを作ります:


  #!/usr/bin/perl -w
use HTML::Template;

  # open the html template
my $template = HTML::Template->new(filename => 'test.tmpl');

  # fill in some parameters
$template->param(HOME => $ENV{HOME});
$template->param(PATH => $ENV{PATH});

  # send the obligatory Content-Type and print the template output
print "Content-Type: text/html\n\n", $template->output;

これですべてがうまくいっていれば、ブラウザでそのCGIにいくと、
以下のようなものが表示されます:


  My Home Directory is /home/some/directory
My Path is set to /bin;/usr/bin


以上

0 件のコメント:

Blog Archive