treebird/perl/template_helpers.pm
nekobit 670a4021b8 Perlify meta pages
FossilOrigin-Name: d2af76ca88f30b880bf7d22f8247dc17b370dfa83700adef18798e33b3e85a37
2022-08-19 15:06:19 +00:00

55 lines
992 B
Perl

package template_helpers;
use strict;
use warnings;
use Exporter 'import';
our @EXPORT = qw( to_template simple_page );
use string_helpers 'simple_escape';
my $template = Template->new(
{
INTERPOLATE => 1,
POST_CHOMP => 1,
EVAL_PERL => 1,
TRIM => 1
});
sub pretty_error($)
{
my $error = simple_escape(shift);
<< "END_ERROR";
<span class="e-error error-pad">
$error
</span>
END_ERROR
}
sub to_template
{
my ($vars, $data) = @_;
my $result;
return 0 unless ref $data;
return 0 unless ref $vars;
# TODO HTML error formatting
$template->process($data, $vars, \$result) ||
return pretty_error($template->error());
$result;
}
# Generic simple page with only session data and pages.
# Pretty commonly done, so useful function.
sub simple_page
{
my ($ssn, $data, $page) = @_;
my %vars = (
prefix => '',
ssn => $ssn,
);
to_template(\%vars, \$data->{$page});
}