View | Details | Raw Unified | Return to bug 33030
Collapse All | Expand All

(-)a/Koha/TemplateUtils.pm (+100 lines)
Line 0 Link Here
1
package Koha::TemplateUtils;
2
3
# Copyright ByWater Solutions 2023
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Carp qw( croak );
23
use Try::Tiny;
24
use Template;
25
26
use C4::Context;
27
28
use vars qw(@ISA @EXPORT_OK);
29
30
BEGIN {
31
    require Exporter;
32
    @ISA       = qw(Exporter);
33
    @EXPORT_OK = qw( process_tt );
34
}
35
36
=head1 NAME
37
38
Koha::TemplateUtils
39
40
A module to centralize and standardize processing of template toolkit syntax
41
for uses other than slips and notices.
42
43
=head1 DESCRIPTION
44
45
Koha has evolved to use Template Toolkit for many uses outside of generating slips and notices for patrons. Historically, each of these areas processed template toolkit in a slightly different way. This module is meant to allow Template Toolkit syntax to be processed in a standard and generic way such that all non-notice TT syntax is handled and processed the consistently.
46
47
=head2 process_tt
48
49
$processed = process_tt($template, $vars);
50
51
Process the given Template Toolkit string, passing the given hashref of vars to the template, returning the processed string.
52
53
=cut
54
55
sub process_tt {
56
    my ( $template, $vars ) = @_;
57
58
    if ( index( $template, '[%' ) != -1 ) { # Much faster than regex
59
        my $use_template_cache = C4::Context->config('template_cache_dir')
60
          && defined $ENV{GATEWAY_INTERFACE};
61
62
        my $tt = Template->new(
63
            {
64
                EVAL_PERL   => 1,
65
                ABSOLUTE    => 1,
66
                PLUGIN_BASE => 'Koha::Template::Plugin',
67
                COMPILE_EXT => $use_template_cache ? '.ttc' : '',
68
                COMPILE_DIR => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
69
                FILTERS      => {},
70
                ENCODING     => 'UTF-8',
71
            }
72
        ) or die Template->error();
73
74
        my $schema = Koha::Database->new->schema;
75
        my $output;
76
77
        $schema->txn_begin;
78
        try {
79
            $tt->process( \$template, $vars, \$output );
80
        }
81
        catch {
82
            croak "ERROR PROCESSING TEMPLATE: $_ :: " . $template->error();
83
        }
84
        finally {
85
            $schema->txn_rollback;
86
        };
87
88
        return $output;
89
    } else {
90
        return $template;
91
    }
92
}
93
94
=head1 AUTHOR
95
96
Kyle M Hall <kyle@bywatersolutions.com>
97
98
=cut
99
100
1;
(-)a/t/db_dependent/Letters/TemplateToolkit.t (-2 / +1 lines)
Lines 19-25 Link Here
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
# along with Koha; if not, see <http://www.gnu.org/licenses>.
20
20
21
use Modern::Perl;
21
use Modern::Perl;
22
use Test::More tests => 31;
22
use Test::More tests => 32;
23
use Test::MockModule;
23
use Test::MockModule;
24
use Test::Warn;
24
use Test::Warn;
25
25
26
- 

Return to bug 33030