File: 33_plugins.t

package info (click to toggle)
libhtml-template-compiled-perl 1.003-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 772 kB
  • sloc: perl: 4,759; makefile: 5
file content (115 lines) | stat: -rw-r--r-- 2,869 bytes parent folder | download | duplicates (2)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115

use warnings;
use strict;
use lib 't';
use Test::More tests => 5;
use HTML::Template::Compiled;
use HTC_Utils qw($cache $tdir &cdir);

for (0..1) {
    my $plug = bless(
        {}, 'HTC_Test'
    );
    HTML::Template::Compiled->register($plug);
    sub HTC_Test::register {
        my ($class) = @_;
        my %plugs = (
            escape => {
                TESTING => sub {
                    my ($arg) = @_;
                    return "$_$arg$arg";
                },
            },
        );
        return \%plugs;

    }
    my $htc = HTML::Template::Compiled->new(
        scalarref => \<<'EOM',
<%= foo escape=testing %>
EOM
        plugin => [$plug],
        debug    => 0,
        cache => 0,
    );
    my $string = 'string';
    $htc->param(
        foo => $string,
    );

    my $out = $htc->output;
    #print "out: $out\n";
    cmp_ok($out, '=~', "$_$string$string", "plugin as object $_");
}

{
    my $plug = bless(
        {
            'lang' => 'en',
            'map' => {
                en => {
                    HELLO_WORLD => 'Hello world',
                },
                de => {
                    HELLO_WORLD => 'Hallo Welt',
                },
                es => {
                    HELLO_WORLD => 'Hola Mundo',
                },
            },
        }, 'HTC_Test2'
    );
    HTML::Template::Compiled->register($plug);
    sub HTC_Test2::translate {
        my ($self, $id) = @_;
        return $self->{map}->{ $self->{lang} }->{$id};
    }
    sub HTC_Test2::register {
        my ($class) = @_;

        my %plugs = (
            tagnames => {
                HTML::Template::Compiled::Token::OPENING_TAG() => {
                    TRANSLATE => [sub { exists $_[1]->{ID} }, 'ID'],
                },
            },
            compile => {
                TRANSLATE => {
                    open => sub {
                        my ($htc, $token, $args) = @_;
                        my $OUT = $args->{out};
                        my $attr = $token->get_attributes;
                        my $expression = <<"EOM";
    $OUT "Translation of $attr->{ID}: ";
    $OUT \$t->get_plugin('HTC_Test2')->translate('\Q$attr->{ID}\E');
EOM
                        return $expression;
                    },
                },
            },
        );
        return \%plugs;
    }
    my $htc = HTML::Template::Compiled->new(
        scalarref => \<<'EOM',
<%translate id="HELLO_WORLD" %>
EOM
        plugin => [$plug],
        debug    => 0,
        cache => 0,
    );
    my $string = 'string';
    for my $lang (qw/ en de es /) {
        $plug->{lang} = $lang;
        my $translated = $plug->{map}->{$lang}->{HELLO_WORLD};
        my $out = $htc->output;
        #print "out: $out\n";
        cmp_ok($out, '=~', "$translated", "plugin as object $lang");
    }


}

HTML::Template::Compiled->clear_filecache($cache);