/usr/share/perl5/Template/Alloy/Context.pm is in libtemplate-alloy-perl 1.020-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | package Template::Alloy::Context;
=head1 NAME
Template::Alloy::Context - Provide a TT style context
=cut
use strict;
use warnings;
use Template::Alloy;
our $VERSION = $Template::Alloy::VERSION;
use vars qw($AUTOLOAD);
###----------------------------------------------------------------###
sub new {
my $class = shift;
my $self = shift || {};
die "Missing _template" if ! $self->{'_template'};
return bless $self, $class;
}
sub _template { shift->{'_template'} || die "Missing _template" }
sub template {
my ($self, $name) = @_;
return $self->_template->{'BLOCKS'}->{$name} || $self->_template->load_template($name);
}
sub config { shift->_template }
sub stash {
my $self = shift;
return $self->{'stash'} ||= bless {_template => $self->_template}, 'Template::Alloy::_ContextStash';
}
sub insert {
my ($self, $file) = @_;;
my $t = $self->_template;
my $ref = $t->slurp($t->include_filename($file));
return $$ref;
}
sub eval_perl { shift->_template->{'EVAL_PERL'} }
sub process {
my $self = shift;
my $ref = shift;
my $args = shift || {};
$self->_template->set_variable($_, $args->{$_}) for keys %$args;
my $out = '';
$self->_template->_process($ref, $self->_template->_vars, \$out);
return $out;
}
sub include {
my $self = shift;
my $ref = shift;
my $args = shift || {};
my $t = $self->_template;
my $swap = $t->{'_vars'};
local $t->{'_vars'} = {%$swap};
$t->set_variable($_, $args->{$_}) for keys %$args;
my $out = ''; # have temp item to allow clear to correctly clear
eval { $t->_process($ref, $t->_vars, \$out) };
if (my $err = $@) {
die $err if ! UNIVERSAL::can($err, 'type') || $err->type !~ /return/;
}
return $out;
}
sub define_filter {
my ($self, $name, $filter, $is_dynamic) = @_;
$filter = [ $filter, 1 ] if $is_dynamic;
$self->define_vmethod('filter', $name, $filter);
}
sub filter {
my ($self, $name, $args, $alias) = @_;
my $t = $self->_template;
my $filter;
if (! ref $name) {
$filter = $t->{'FILTERS'}->{$name} || $Template::Alloy::FILTER_OPS->{$name} || $Template::Alloy::SCALAR_OPS->{$name};
$t->throw('filter', $name) if ! $filter;
} elsif (UNIVERSAL::isa($name, 'CODE') || UNIVERSAL::isa($name, 'ARRAY')) {
$filter = $name;
} elsif (UNIVERSAL::can($name, 'factory')) {
$filter = $name->factory || $t->throw($name->error);
} else {
$t->throw('undef', "$name: filter not found");
}
if (UNIVERSAL::isa($filter, 'ARRAY')) {
$filter = ($filter->[1]) ? $filter->[0]->($t->context, @$args) : $filter->[0];
} elsif ($args && @$args) {
my $sub = $filter;
$filter = sub { $sub->(shift, @$args) };
}
$t->{'FILTERS'}->{$alias} = $filter if $alias;
return $filter;
}
sub define_vmethod { shift->_template->define_vmethod(@_) }
sub throw {
my ($self, $type, $info) = @_;
if (UNIVERSAL::can($type, 'type')) {
die $type;
} elsif (defined $info) {
$self->_template->throw($type, $info);
} else {
$self->_template->throw('undef', $type);
}
}
sub AUTOLOAD { shift->_template->throw('not_implemented', "The method $AUTOLOAD has not been implemented") }
sub DESTROY {}
###----------------------------------------------------------------###
package Template::Alloy::_ContextStash;
use vars qw($AUTOLOAD);
sub _template { shift->{'_template'} || die "Missing _template" }
sub get {
my ($self, $var) = @_;
if (! ref $var) {
if ($var =~ /^\w+$/) { $var = [$var, 0] }
else { $var = $self->_template->parse_expr(\$var, {no_dots => 1}) }
}
return $self->_template->play_expr($var, {no_dots => 1});
}
sub set {
my ($self, $var, $val) = @_;
if (! ref $var) {
if ($var =~ /^\w+$/) { $var = [$var, 0] }
else { $var = $self->_template->parse_expr(\$var, {no_dots => 1}) }
}
$self->_template->set_variable($var, $val, {no_dots => 1});
return $val;
}
sub AUTOLOAD { shift->_template->throw('not_implemented', "The method $AUTOLOAD has not been implemented") }
sub DESTROY {}
###----------------------------------------------------------------###
1;
__END__
=head1 DESCRIPTION
Template::Alloy::Context provides compatibility with Template::Context
and filters that require Template::Context.
=head1 TODO
Document all of the methods.
=head1 AUTHOR
Paul Seamons <paul@seamons.com>
=head1 LICENSE
This module may be distributed under the same terms as Perl itself.
=cut
|