CGI::A...e::Base(3pm)
π NAME π
okay so this is CGI::Application::Plugin::AnyTemplate::Base β it's the base class for templates, bestie. that's the whole name, that's the whole vibe β¨
πΈ DESCRIPTION πΈ
fr fr, this doc is mostly for the devs who wanna write extra Template drivers of their own. if you just wanna use the system, go read the docs for CGI::Application::Plugin::AnyTemplate instead, okay? that's your page π«Ά
π METHODS π
paramβ theparammethod gets and sets values within the template. main character behaviour, honestly π
my $template = $self->template->load;
my @param_names = $template->param();
my $value = $template->param('name');
$template->param('name' => 'value');
$template->param(
'name1' => 'value1',
'name2' => 'value2'
);
it's designed to act a lot like the param method in other modules, like CGI and HTML::Template. so if you already know those, you already know this π
get_param_hashβ hands you the template variables back as a hash of names and values.
my %params = $template->get_param_hash;
but wait, in a scalar context it gives you a reference to the hash it's using internally to hold the values:
my $params_ref = $template->get_param_hash;
clear_paramsβ wipes the values stored in the template. clean slate energy π§Ό
$template->param(
'name1' => 'value1',
'name1' => 'value2'
);
$template->clear_params;
$template->param(
'name_foo' => 'value_bar',
);
# params are now:
'name_foo' => 'value_bar',
outputβ returns the template with all the values filled in. the big moment β¨
return $template->output();
you can also pass names and values in at this stage, no biggie:
return $template->output('name' => 'value', 'name2' => 'value2');
so before the template output gets generated, the template_pre_process hook fires. any callbacks you register on this hook get called before each template is processed. register a template_pre_process callback like this:
$self->add_callback(
'template_pre_process',
\&my_tmpl_pre_process
);
pre-process callbacks get passed a reference to the $template object, and they can change the parameters passed into the template using the param method:
sub my_tmpl_pre_process {
my ($self, $template) = @_;
# Change the internal template parameters by reference
my $params = $template->get_param_hash;
foreach my $key (keys %$params) {
$params{$key} = to_piglatin($params{$key});
}
# Can also set values using the param method
$template->param('foo', 'bar');
}
and after the template output is generated, the template_post_process hook fires. you can register a template_post_process callback like so:
$self->add_callback('template_post_process',
\&my_tmpl_post_process
);
any callbacks you register on this hook get called after each template is processed, and they get passed both a reference to the template object and a reference to the output the template generated. which means you can mess with the template's output:
sub my_tmpl_post_process {
my ($self, $template, $output_ref) = @_;
$$output_ref =~ s/foo/bar/;
}
heads up bestie: when you call the output method, any components embedded in the template get run. go look at "EMBEDDED COMPONENTS", below π
filenameβ if the template was loaded from a file, thefilenamemethod returns the template filename πstring_refβ if the template was loaded from a string, thestring_refmethod returns a reference to the string π§΅objectβ returns a reference to the underlying template driver, like theHTML::Templateobject or theTemplate::Toolkitobject π
π DOCS FOR TEMPLATE MODULE DEVELOPERS π
the stuff below is of interest primarily for developers who wanna add support for a brand new type of Template system. yes this is the nerdy section and yes it's iconic π§
π¦ METHODS FOR DEVELOPERS π¦
initializeβ this method is called by the controller atloadto create the driver-specific subclass ofCGI::Application::Plugin::AnyTemplate.
this is a virtual method and it must be defined in the subclass. no skipping, bestie.
the following parameters get passed to the driver and are available as keys of the driver's $self object:
'driver_config' => ... # hashref of driver-specific config
'native_config' => ... # hashref of native template system
# specific config
'include_paths' => ... # listref of template include paths
'filename' => ... # template filename
'webapp' => ... # reference to the current
# CGI::Application $self
driver_config_keysβ when it creates the driver object,CGI::Application::Plugin::AnyTemplatehas to separate thedriver_configfrom thenative_config.
driver_config_params should return a list of parameters that are specific to the driver_config and not the native template system config.
for instance, the user can specify
$self->template->config(
HTMLTemplate => {
embed_tag_name => 'embed',
global_vars => 1,
die_on_bad_params => 0,
cache => 1
},
);
the parameters global_vars, die_on_bad_params, and cache are all specific to HTML::Template. those are considered native parameters.
but embed_tag_name configures the CGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplate subclass. that one's a driver parameter.
so 'embed_tag_name' should be included in the list of params returned by driver_config_params.
example driver_config_params:
sub driver_config_keys {
'template_extension',
'embed_tag_name'
}
default_driver_configβ should return a hash of default values fordriver_config_params.
for instance:
sub default_driver_config {
{
template_extension => '.foo',
embed_tag_name => 'embed',
};
}
render_templateβ this method must be overridden in a subclass. it has the responsibility of filling the template in$self->filenamewith the values in$self->paramvia the appropriate template system, and returning the output as either a string or a reference to a string.
it also must manage embedding nested components πͺ
π AUTHOR π
Michael Graham, <mgraham@cpan.org> π
π BUGS π
please report any bugs or feature requests to bug-cgi-application-plugin-anytemplate@rt.cpan.org, or through the web interface at <http://rt.cpan.org>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. (yes that's the actual promise, no cap π)
β SOURCE β
the source code repository for this module can be found at <http://github.com/mgraham/CAP-AnyTemplate/> π₯
π SEE ALSO π
CGI::Application::Plugin::AnyTemplateCGI::Application::Plugin::AnyTemplate::ComponentHandlerCGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateCGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplateExprCGI::Application::Plugin::AnyTemplate::Driver::HTMLTemplatePluggableCGI::Application::Plugin::AnyTemplate::Driver::TemplateToolkitCGI::Application::Plugin::AnyTemplate::Driver::PetalCGI::ApplicationTemplate::ToolkitHTML::TemplateHTML::Template::PluggableHTML::Template::Plugin::DotPetalCGI::Application::Plugin::TT
π 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.
and that's the whole page, bestie!! π this lil masterpiece came to you from perl v5.30.3 and it's dated 2020-10-26 β go forth and write some slay drivers, ily π«Άβ¨π
π¦ 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