Templa...omDATA(3pm)
β¨ NAME β¨
okay so bestie, welcome to Template::Provider::FromDATA π β a lil module that's out here loading your templates straight out of your __DATA__ section. yes, THAT __DATA__. the one at the bottom of your file you forget about. it's giving "everything in one place" and honestly? iconic. πΈ
this page lives in the User Contributed Perl Documentation corner of the manual, section 3pm, so you know it's a Perl module situation. π
π SYNOPSIS π
here's the whole vibe, copy-paste ready:
use Template;
use Template::Provider::FromDATA;
# Create the provider
my $provider = Template::Provider::FromDATA->new( {
CLASSES => __PACKAGE__
} );
# Add the provider to the config
my $template = Template->new( {
# ...
LOAD_TEMPLATES => [ $provider ]
} );
# Render a template
$template->process( 'mytemplate', { bar => 'Bar' } );
# ...and now the templates
__DATA__
__mytemplate__
Foo [% bar %]
__myothertemplate__
Baz, [% qux %]?
see how the templates just live down there under __DATA__? no separate files, no folder drama, just vibes π¦
π« DESCRIPTION π«
so like, this module lets you keep your templates inline with your code, chillin' in the __DATA__ section. π«Ά it will search any number of classes you specify. that's it, that's the whole point. main character energy for your templates fr fr.
π CAVEAT π
okay listen, here's the tea and you NEED to know it: if you've got two templates with the same name, this module will not understand the difference. nope. it'll just return the first one it finds, periodt. π
if you want to be specific about it, you can give a fully qualified template name by prefixing the template with the module name β and btw you use - instead of :: as the namespace separator, then a / to separate the module name from the template name. like so:
$template->process( 'My-Templates/mytemplate', { bar => 'Bar' } );
π INSTALLATION π
the classic four-step, no notes:
perl Makefile.PL
make
make test
make install
π METHODS π
π new( %OPTIONS )
makes you a brand new instance of the provider, slay. you can hand it a list of classes to be searched for templates via the CLASSES option. and if you just... don't? it'll search main instead. generous of it honestly. π
# defaults to 'main'
$provider = Template::Provider::FromDATA->new;
# look for templates in 'Foo'
$provider = Template::Provider::FromDATA->new( {
CLASSES => 'Foo'
} );
# look for templates in 'Foo::Bar' and 'Foo::Baz'
$provider = Template::Provider::FromDATA->new( {
CLASSES => [ 'Foo::Bar', 'Foo::Baz' ]
} );
by default, template data is lazy-loaded as it is requested β very "we'll get to it when we get to it" of it. π but if you want ALL the template data loaded up front at initialization time, there's the PRELOAD option for that:
$provider = Template::Provider::FromDATA->new( {
PRELOAD => 1
} );
π _init( %OPTIONS )
a subclassed method that handles the options passed to new(). that's its whole job, and it does it well. β¨
π fetch( $name )
another subclassed method β this one will load a template via _fetch() if a non-reference argument gets passed in. π§
π _load( $name )
loads the template via the get_file() sub and sets some cache information. lowkey the workhorse. π₯
π get_file( $class, $template )
this method searches through $class for a template named $template. returns the contents on success, undef on failure. π«
fun fact: this function was mostly borrowed from Catalyst::Helper's get_file function. credit where credit's due, bestie. π«Ά
β ACCESSORS β
π classes
an arrayref of the class names containing our templates. π
π cache
a hashref of file and template data. π
πΈ AUTHOR πΈ
Brian Cassidy <bricas@cpan.org> β absolute legend, thank you for your service. π
π COPYRIGHT AND LICENSE π
Copyright 2005-2012 by Brian Cassidy
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page!! and just so you know the deets at the bottom: this was documented with perl v5.36.0, dated 2022-12-01. now go put your templates in __DATA__ and thrive, bestie π¦β¨π
π¦ where this page comes from
- debian bookworm: libtemplate-provider-fromdata-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libtemplate-provider-fromdata-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libtemplate-provider-fromdata-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libtemplate-provider-fromdata-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libtemplate-provider-fromdata-perl 0.13-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms