]> iEval git - plack-app-gruntmaster.git/blame_incremental - lib/Gruntmaster/Page/Ct.pm
Centralize template cooking and introduce reloadable templates
[plack-app-gruntmaster.git] / lib / Gruntmaster / Page / Ct.pm
... / ...
CommitLineData
1package Gruntmaster::Page::Ct;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
7our @EXPORT_OK = qw/generate/;
8our $VERSION = '0.001';
9
10use HTML::Template::Compiled;
11use POSIX qw/strftime/;
12use Gruntmaster::Page::Common qw/cook_templates reload_templates/;
13use Gruntmaster::Data qw/contest_name contest_start contest_end contest_owner/;
14
15my %orig_templates = (
16 en => <<'HTML',
17<tmpl_if running>
18<h1>Running contests</h1>
19<table border>
20<thead>
21<tr><th>Name<th>Start date<th>End date<th>Owner
22<tbody>
23<tmpl_loop running><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
24<td><tmpl_var start>
25<td><tmpl_var end>
26<td><tmpl_var owner>
27</tmpl_loop>
28</table>
29</tmpl_if>
30
31<tmpl_if pending>
32<h1>Pending contests</h1>
33<table border>
34<thead>
35<tr><th>Name<th>Start date<th>End date<th>Owner
36<tbody>
37<tmpl_loop pending><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
38<td><tmpl_var start>
39<td><tmpl_var end>
40<td><tmpl_var owner>
41</tmpl_loop>
42</table>
43</tmpl_if>
44
45<tmpl_if finished>
46<h1>Finished contests</h1>
47<table border>
48<thead>
49<tr><th>Name<th>Start date<th>End date<th>Owner
50<tbody>
51<tmpl_loop finished><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
52<td><tmpl_var start>
53<td><tmpl_var end>
54<td><tmpl_var owner>
55</tmpl_loop>
56</table>
57</tmpl_if>
58HTML
59);
60
61my %templates = cook_templates %orig_templates, ct => 'Contests';
62
63sub generate{
64 %templates = cook_templates %orig_templates, ct => 'Contests' if reload_template;
65 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$_[1]});
66
67 my (@running, @pending, @finished);
68 for (contests) {
69 my $ct = { id => $_,
70 name => contest_name,
71 start => strftime ('%c', localtime contest_start),
72 end => strftime ('%c', localtime contest_end),
73 owner => contest_owner };
74
75 my $time = time;
76 push @pending, $ct if time < contest_start;
77 push @running, $ct if time >= contest_start && time < contest_end;
78 push @finished, $ct if time > contest_end;
79 }
80
81 $htc->param(running => \@running);
82 $htc->param(pending => \@pending);
83 $htc->param(finished => \@finished);
84 $htc->output
85}
86
871
This page took 0.017029 seconds and 4 git commands to generate.