| 1 | =head1 NAME | ||
| 2 | |||
| 3 | Template::TAL::Provider::Disk - find template files on disk | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | my $provider = Template::TAL::Provider::Disk->new->include_path( "./templates" ); | ||
| 8 | my $ttt = $provider->get_template("foo.tal"); | ||
| 9 | |||
| 10 | =head1 DESCRIPTION | ||
| 11 | |||
| 12 | A Template::TAL::Provider that creates template objects from files on disk. | ||
| 13 | |||
| 14 | Given a list of include paths, it will search them in order, looking for a | ||
| 15 | named template, then return it in a L<Template::TAL::Template> object. | ||
| 16 | |||
| 17 | This is the default provider and an instance will be created for each | ||
| 18 | Template::TAL object unless an alternative provider is specified, meaning | ||
| 19 | you very rarely have to use this class directly. | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | package Template::TAL::Provider::Disk; | ||
| 24 | use warnings; | ||
| 25 | use strict; | ||
| 26 | use Carp qw( croak ); | ||
| 27 | use File::Spec::Functions; | ||
| 28 | use base qw( Template::TAL::Provider ); | ||
| 29 | use Cwd qw( abs_path ); | ||
| 30 | use Template::TAL::Template; | ||
| 31 | |||
| 32 | =head1 METHODS | ||
| 33 | |||
| 34 | =over | ||
| 35 | |||
| 36 | =item include_path | ||
| 37 | |||
| 38 | When called with no argument returns an arrayref of the search paths. May | ||
| 39 | be called with an argument, either a simple string or an arrayref, to set | ||
| 40 | a new search path / new search paths respectivly and return self. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | sub include_path { | ||
| 45 | my $self = shift; | ||
| 46 | |||
| 47 | unless (@_) { | ||
| 48 | return $self->{include_path} ||= []; | ||
| 49 | } | ||
| 50 | |||
| 51 | # shallow copy to avoid unforseen modification | ||
| 52 | $self->{include_path} = [ | ||
| 53 | ref($_[0]) eq 'ARRAY' ? @{ $_[0] } : ($_[0]) | ||
| 54 | ]; | ||
| 55 | |||
| 56 | return $self; | ||
| 57 | } | ||
| 58 | |||
| 59 | =item get_template( name ) | ||
| 60 | |||
| 61 | searches the include path for files of the given name, and returns the first | ||
| 62 | found as a Template::TAL::Template object. Will die if it can't find a | ||
| 63 | template file with the given name or is unable to open the named file. | ||
| 64 | |||
| 65 | =cut | ||
| 66 | |||
| 67 | sub get_template { | ||
| 68 | my ($self, $name) = @_; | ||
| 69 | for my $path (@{ $self->include_path }) { | ||
| 70 | my $filename = catfile( $path, $name ); | ||
| 71 | |||
| 72 | if ( -f $filename ) { | ||
| 73 | my $abs = abs_path( $filename ); | ||
| 74 | croak("not loading $filename from outside include path") | ||
| 75 | unless $abs =~ /^\Q$path/; | ||
| 76 | |||
| 77 | return Template::TAL::Template->new->filename( $filename ); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | croak("no template '$name' found"); | ||
| 81 | } | ||
| 82 | |||
| 83 | =back | ||
| 84 | |||
| 85 | =head1 COPYRIGHT | ||
| 86 | |||
| 87 | Written by Tom Insam, Copyright 2005 Fotango Ltd. All Rights Reserved. | ||
| 88 | |||
| 89 | This program is free software; you can redistribute | ||
| 90 | it and/or modify it under the same terms as Perl itself. | ||
| 91 | |||
| 92 | =head1 BUGS | ||
| 93 | |||
| 94 | Please see L<Template::TAL> for details of how to report bugs. | ||
| 95 | |||
| 96 | Note that currently, this explicitly doesn't do any caching. We rely on | ||
| 97 | the operating system to do that for us. | ||
| 98 | |||
| 99 | =head1 SEE ALSO | ||
| 100 | |||
| 101 | L<Template::TAL>, L<Template::TAL::Provider> | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | 1; |