File: templating-mojo.cgi

package info (click to toggle)
libcgi-tiny-perl 1.003-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 516 kB
  • sloc: perl: 1,307; makefile: 2
file content (30 lines) | stat: -rwxr-xr-x 672 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
use CGI::Tiny;
use Mojo::Template;
use Mojo::File 'curfile';
use Mojo::Loader 'data_section';

cgi {
  my $cgi = $_;

  my $mt = Mojo::Template->new(auto_escape => 1, vars => 1);

  my $foo = $cgi->query_param('foo');

  # from templates/
  my $template_path = curfile->sibling('templates', 'index.html.ep');
  my $output = $mt->render_file($template_path, {foo => $foo});

  # or from __DATA__
  my $template = data_section __PACKAGE__, 'index.html.ep';
  my $output = $mt->render($template, {foo => $foo});

  $cgi->render(html => $output);
};

__DATA__
@@ index.html.ep
<html><body><h1><%= $foo %></h1></body></html>