CGI::A...oolkit(3pm)
π NAME π
CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit β aka the Template::Toolkit plugin to AnyTemplate. Yes bestie, the whole long name, that's just how CPAN does it π
β¨ DESCRIPTION β¨
So this is a driver for CGI::Application::Plugin::AnyTemplate, and what it does is hand over the implementation details that are specific to rendering templates through the Template::Toolkit templating system. That's the whole vibe.
And here's the thing β all the AnyTemplate drivers are designed to be used exactly the same way. So for general usage instructions, go read the docs of CGI::Application::Plugin::AnyTemplate. It's giving very "same energy, different engine" fr fr π¦
π EMBEDDED COMPONENT SYNTAX (Template::Toolkit) π
The Template::Toolkit syntax for embedding components goes like this:
[% CGIAPP.embed("some_run_mode", param1, param2, 'literal string3') %]
And you can override it with these configuration variables:
embed_tag_name # default 'CGIAPP'
So for instance, if you set these values in your configuration file:
embed_tag_name 'MYAPP'
Then your embedded component tag ends up looking like:
[% MYAPP.embed("some_run_mode") %]
Iconic, honestly π
π TT OBJECT CACHING (singleton support) π
πΈ Introduction πΈ
Okay so picture this: you're in a persistent environment. Instead of making a brand new Template::Toolkit object every single time you fill a template, it is so much more efficient to load one single Template::Toolkit object and then use that one object to render all of your templates. Efficiency queen behavior π
BUT. Plot twist π«’ In a persistent environment you might have several different applications running at the same time, and they might all need to set different Template::Toolkit options (stuff like POST_CHOMP, etc.).
So by default, when the TemplateToolkit driver creates a Template::Toolkit object, it caches it. And from that point on, whenever the same application needs a Template::Toolkit object, the driver just uses the cached object instead of building a new one. Reuse, recycle, slay β»οΈβ¨
π¦ Multiple Applications in a Shared Persistent Environment π¦
An attempt is made to stop different applications from sharing the same TT object. Boundaries, bestie π
Internally, the TT objects are stored in a private hash</> keyed by the web application's class name.
You can explicitly specify the class name when you call config:
$self->template->config(
type => 'TemplateToolkit',
TemplateToolkit => {
storage_class => 'My::Project',
},
);
If you don't specify the class name, then the package containing the subroutine that called config is the one that gets used. Like so:
package My::Project;
sub setup {
my $self = shift;
$self->template->config( # My::Project is used to store
type => 'TemplateToolkit', # cached TT object
);
}
A typical CGI::Application module hierarchy looks like this:
CGI::Application
My::Project
My::Webapp
In this hierarchy, it makes sense to store the cached TT object in My::Project. To make that happen, either call $self->template->config from within My::Project, or explicitly name the storage_class when you call $self->template->config. Your call, your rules π
π Disabling TT Object Caching π
You can disable Template::Toolkit object caching entirely by handing a false value to the object_caching driver config parameter:
$self->template->config(
type => 'TemplateToolkit',
TemplateToolkit => {
object_caching => 0,
},
);
π TT Object Caching and Include Paths π
The include_paths driver config parameter is not cached β it gets set every single time you call $self->template->load. So you can safely use cached TT objects even if the applications sharing that TT object need different include_paths. No drama, no conflict, we love to see it π«Ά
π CONFIGURATION π
The CGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkit driver accepts the following config parameters:
embed_tag_nameβ the name of the tag used for embedding components. Defaults toCGIAPP.template_extensionβ ifauto_add_template_extensionis true, thenCGI::Application::Plugin::AnyTemplatewill append the value oftemplate_extensiontofilename. By default thetemplate_extensionis.xhtml.emulate_associate_queryβ this feature is now deprecated and will be removed in a future release. If this config parameter is true, thenCGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkitwill copy all of the webapp's query params into the template. This is similar to what would happen if you used HTML::Template'sassociatefeature with the webapp's query object:
my $driver = HTML::Template->new(
associate => $self->query,
);
By default emulate_associate_query is false.
object_cachingβ whether or not to cache the Template::Toolkit object in a persistent environment. By default,object_cachingis enabled. See "TT OBJECT CACHING (singleton support)", above.storage_classβ what class to use as the storage key when object caching is enabled. By default,storage_classdefaults to the package containing the subroutine that called$self->template->config. See "TT OBJECT CACHING (singleton support)", above.
All other configuration parameters are passed on unchanged to Template::Toolkit. No touchy, just pass-through β¨
πΈ CONFIGURING UTF-8 TEMPLATES πΈ
Real talk: AnyTemplate does NOT support Template::Toolkit's binmode option at runtime. Like, at all:
# not possible with AnyTemplate
$tt->process($infile, $vars, $outfile, { binmode => 1 })
|| die $tt->error(), "\n";
# not possible with AnyTemplate
$tt->process($infile, $vars, $outfile, binmode => 1)
|| die $tt->error(), "\n";
# not possible with AnyTemplate
$tt->process($infile, $vars, $outfile, binmode => ':utf8')
|| die $tt->error(), "\n";
Instead, use the ENCODING option in the initial config:
$self->template->config(
default_type => 'TemplateToolkit',
TemplateToolkit => {
ENCODING => 'UTF-8'
}
);
And if you've got a mix of encodings in your templates, then use a separate AnyTemplate configuration for each encoding:
$self->template('ascii')->config(
default_type => 'TemplateToolkit',
);
$self->template('utf-8')->config(
default_type => 'TemplateToolkit',
TemplateToolkit => {
ENCODING => 'UTF-8'
}
);
π§ required_modules π§
The required_modules function returns the modules required for this driver to operate. In this case: Template. Short list, big job, periodt π
π DRIVER METHODS π
initializeβ initializes theTemplateToolkitdriver. See the docs forCGI::Application::Plugin::AnyTemplate::Basefor details.render_templateβ fills the Template::Toolkit object with$self->param. If the paramemulate_associate_queryis true, then set params for each of$self->{'webapp'}->query, mimicking HTML::Template's associate mechanism. Also set up aCGI::Application::Plugin::AnyTemplate::ComponentHandlerobject so that theCGIAPP.embedcallback will work. Returns the output of the filled template as a string reference. See the docs forCGI::Application::Plugin::AnyTemplate::Basefor details.
π₯ SEE ALSO π₯
CGI::Application::Plugin::AnyTemplateCGI::Application::Plugin::AnyTemplate::BaseCGI::Application::Plugin::AnyTemplate::ComponentHandlerCGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateCGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExprCGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggableCGI::Application::Plugin::AnyTemplate::Driver::PetalCGI::ApplicationTemplate::ToolkitHTML::TemplateHTML::Template::PluggableHTML::Template::Plugin::DotPetalExporter::RenamingCGI::Application::Plugin::TT
π«Ά ACKNOWLEDGEMENTS π«Ά
Thanks to Cees Hek for discussing the issues of caching in a persistent environment. And also for his excellent CGI::Application::Plugin::TT module, from which I stole ideas and some code β especially the bit about how to change the include path in a TT object after you've initialized it. King behavior, we love a generous icon π
β AUTHOR β
Michael Graham, <mgraham@cpan.org>
π COPYRIGHT & LICENSE π
Copyright 2005 Michael Graham, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay that's the whole page bestie, you ate π This was CGI::A...oolkit(3pm) from perl v5.30.3, dated 2020-10-26. Go render something gorgeous πβ¨π¦
π¦ where this page comes from
- debian bookworm: libcgi-application-plugin-anytemplate-perl 0.18-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcgi-application-plugin-anytemplate-perl 0.18-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu jammy: libcgi-application-plugin-anytemplate-perl 0.18-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcgi-application-plugin-anytemplate-perl 0.18-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcgi-application-plugin-anytemplate-perl 0.18-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcgi-application-plugin-anytemplate-perl 0.18-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