Template::Provider::FromDATA

LTjake on 2005-11-24T20:37:43

I was stealing an idea from Catalyst::Helper and embedding my template files in the __DATA__ section of my code. But i was slightly annoyed by the way i had to read in the wrapper file and assign it as a scalar ref.

I was able to figure out how to make Template::Toolkit read directly from my __DATA__ section -- I figured it was useful enough to release it to CPAN -- Thus, Template::Provider::FromDATA was born.

Here's a little sample script you can play with to see how easy it is.

#!/usr/bin/perl

use strict;
use warnings;

use Template;
use Template::Provider::FromDATA;

my $provider = Template::Provider::FromDATA->new;
my $template = Template->new( {
    LOAD_TEMPLATES => [ $provider ],
    WRAPPER        => 'wrapper'
} );

my $world = shift || 'world';

$template->process( 'hello', { world => $world } );

__DATA__

__wrapper__
header
[% content %]
footer

__hello__
Hello, [% world %]!


see also Inline::TT

rjbs on 2005-11-25T04:51:26

Inline::TT does something similar and fun. I like it a lot. Soon, I will release the code I've got a t work that mixes in to any class to give it a template store that respects inheritance and does some other fun stuff... but first I need to make a few more tests pass!

An alternative...

Alias on 2005-12-12T15:29:24

If the files are needed for something that will be installed to the system, you may want to take a look at File::ShareDir, which demonstrates how to make use of the (little used) shared file facility in installers.