NAME

Top Close Open

Template:TT3::Provider::File - template provider for files

SYNOPSIS

Top Close Open
use Template::TT3::Provider::File;

# specify either a single path...
my $provider = Template::TT3::Provider::File->new(
    path => '/path/to/your/templates',
);

# ...or multiple paths
my $provider = Template::TT3::Provider::File->new(
    path => [
        '/path/to/your/templates/one',
        '/path/to/your/templates/two',
    ],
);

# then fetch templates
my $template = $provider->fetch('example.tt3')
    || die $provider->reason;

DESCRIPTION

Top Close Open

This module is a subclass of Template::TT3::Provider for providing templates from a filesystem. It is a thin wrapper around the Badger::Filesystem::Virtual module.

METHODS

Top Close Open

This module implements the following methods in addition to, or replacing those inherited from the Template::TT3::Provider , Template::TT3::Base and Badger::Base base classes.

init()

Top Close Open

Custom initialisation method called by the new() constructor method inherited from Badger::Base . It expects to be passed either of a path or root parameter (the two are interchangeable) indicating the root directory of the templates.

my $provider = Template::TT3::Provider::File->new(
    path => '/path/to/your/templates',
);

It creates a virtual filesystem object to manage the files under that root directory. Files are always resolved relative to this root directory. Files located outside of the root directory will not be accessible.

You can specify multiple directories by providing an array reference as the path .

my $provider = Template::TT3::Provider::File->new(
    path => [ 
        '/path/to/your/templates/one',
        '/path/to/your/templates/two',
    ],
);

fetch($uri)

Top Close Open

This method fetches a template from the filesystem identified by the $uri argument. This should be a regular file path, e.g. bar.tt3, /foo/bar.tt3, etc. Both relative and absolute paths are resolved with respect to the root path of the virtual filesystem.

my $template = $provider->fetch('example.tt3')
    || die $provider->reason;

It returns a hash array containing a file item which references a Badger::Filesystem::File object and a uri indicating the definitive path (i.e. the absolute path, including the root directory of the virtual filesystem) of the template. e.g.

{
    file => $VFS->file('/local/path.tt3'),
    uri  => '/path/to/your/templates/two/local/path.tt3',
}

If the file cannot be found then the method returns undef. An error message (or strictly speaking, a decline message, as no error is deemed to have occurred) is available via the reason() method inherited from Badger::Base .

If an error occurs then an exception will thrown.

CONFIGURATION OPTIONS

Top Close Open

root / path

Top Close Open

The root parameter is used to specify the path to the template directory. It may be specified as a single directory name, a Badger::Filesystem::Directory object) or as a reference to an array of either of the above.

Here's an example using a regular directory path.

my $provider = Template::TT3::Provider::File->new(
    root => '/path/to/your/templates',
);

And here's the same thing using a Badger::Filesystem::Directory object.

use Badger::Filesystem 'Dir';

my $provider = Template::TT3::Provider::File->new(
    root => Dir('/path/to/your/templates'),
);

This example shows two directory paths being specified.

# ...or an array reference to multiple paths...
my $provider = Template::TT3::Provider::File->new(
    root => [ 
        '/path/to/your/templates/one',
        '/path/to/your/templates/two',
    ],
);

And here's the same thing using Badger::Filesystem::Directory objects.

use Badger::Filesystem 'Dir';

my $tdir = Dir('/path/to/your/templates');

my $provider = Template::TT3::Provider::File->new(
    root => [ 
        $tdir->dir('one'),      # /path/to/your/templates/one
        $tdir->dir('two'),      # /path/to/your/templates/two
    ],
);

path is an alias for root. The Badger::Filesystem::Virtual module expects a root parameter, but path is the more familiar terms used in TT3 (template_path, plugin_path, etc). So we support either

AUTHOR

Top Close Open

Andy Wardley http://wardley.org

COPYRIGHT

Top Close Open

Copyright (C) 1996-2009 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


http://tt3.template-toolkit.org/docs/Template/TT3/Provider/File.pm last modified 2009-12-23 11:38:05