Templa...omDATA(3pm)

πŸ“¦
version 0.13 Β· 2 versions
  • 0.13 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.13 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

✨ 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 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