Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Distributing/Accessing Template::Toolkit Templates

by three18ti (Scribe)
on Nov 05, 2013 at 06:20 UTC ( #1061248=perlquestion: print w/replies, xml ) Need Help??
three18ti has asked for the wisdom of the Perl Monks concerning the following question:


I would like to include some templates with a Module I am developing, but I'm not sure how I would include them such that when the module is installed the templates are accessible.

I use Dist::Zilla to mint the skeleton and build/test/release the module.

So if I put my templates in lib/Foo/Templates/*.tt, what parameter to I feed my Template->new to to tell it to search in the $default_locations/Foo/Templates/? Ideally, I would prefer not to have to specify all the locations, because there are a number of pitfalls with that solution (what if module is installed in non standard location, what if a windows user installs the module, etc.)

I think this is what tells it to search my local build directory:

my $tt = Template->new({ INCLUDE_PATH => ['lib/Foo/Templates'], }) || die "$Template::ERROR\n";

Thanks Monks!

Replies are listed 'Best First'.
Re: Distributing/Accessing Template::Toolkit Templates
by GrandFather (Sage) on Nov 05, 2013 at 08:26 UTC

    If you install the templates in a directory relative to the directory containing your module then you can use %INC to get the path:

    use warnings; use strict; use Foo; (my $fooDir = $INC{''}) =~ s/Foo\.pm$//; my $tmplDir = "${fooDir}Templates";
    True laziness is hard work

      Hey, that's pretty clever. Are there any pitfals with this method? File::ShareDir seems like it might be the better option, unless for some reason you're limited in the modules you can use...

      Thanks for the reply!

        I'd go with File::Share. I was unaware of it. It looks a better fit for your distribution problem.

        True laziness is hard work
Re: Distributing/Accessing Template::Toolkit Templates (File::ShareDir)
by Anonymous Monk on Nov 05, 2013 at 08:37 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1061248]
Approved by GrandFather
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2018-05-26 21:10 GMT
Find Nodes?
    Voting Booth?