Mas sobre CGIs con Templates

$ cat -n get_projects.pl
 1  #!/usr/bin/perl
 2  use strict;
 3  use warnings;
 4  use Template;
 5  use CGI;
 6
 7  $| = 1;
 8  print "Content-type: text/html\n\n";
 9
10  my $file = 'userinfo.html';
11  my $vars = {
12      'version'  => 3.14,
13      'days'     => [ qw( mon tue wed thu fri sat sun ) ],
14      'worklist' => \&get_user_projects,
15      'cgi'      => CGI->new(),
16      'me'       => {
17          'id'     => 'crguezl',
18          'email'     => 'crguezl@ull.es',
19          'name'   => 'Casiano Rodriguez-Leon',
20      },
21  };
22
23  sub get_user_projects {
24      my $user = shift;
25      my @projects = ({ url => 'http://nereida.deioc.ull.es/~pl', name => 'pl' },
26                      { url => 'http://nereida.deioc.ull.es/~lhp', name => 'lhp'},
27                      { url => 'http://nereida.deioc.ull.es/~pp2', name => 'pp2'},
28      );  # do something to retrieve data
29      return \@projects;
30  }
31
32  my $template = Template->new({
33      PRE_PROCESS  => 'config',
34  });
35
36  $template->process($file, $vars)
37      || die $template->error();
$ cat -n userinfo.html
     1  [% INCLUDE header
     2     title = 'Template Toolkit CGI Test'
     3  %]
     4
     5  Author: <a href="mailto:[% me.email %]">[% me.name %]</a>
     6
     7  <p>This is version [% version %]</p>
     8
     9  <h3>Projects</h3>
    10  <ul>
    11  [% FOREACH project IN worklist(me.id) %]
    12     <li> <a href="[% project.url %]">[% project.name %]</a>
    13  [% END %]
    14  </ul>
    15
    16  [% INCLUDE footer
    17  %]
$ cat -n header
     1  <html>
     2  <head>
     3    <title>[% author %]: [% title %]</title>
     4  </head>
     5
     6  <body bgcolor="[% bgcol %]">
     7    <h1>[% title %]</h1>
$ cat -n footer
     1      <hr />
     2
     3      <div align="middle">
     4        &copy; Copyright [% year %] [% author %]
     5      </div>
     6    </body>
     7  </html>

Casiano Rodríguez León
Licencia de Creative Commons
Principios de Programación Imperativa, Funcional y Orientada a Objetos Una Introducción en Perl/Una Introducción a Perl
por Casiano Rodríguez León is licensed under a Creative Commons Reconocimiento 3.0 Unported License.

Permissions beyond the scope of this license may be available at http://campusvirtual.ull.es/ocw/course/view.php?id=43.
2012-06-19