CGI-Application-Plugin-HTCompiled
view release on metacpan or search on metacpan
lib/CGI/Application/Plugin/HTCompiled.pm view on Meta::CPAN
my $user = My::Users->retrieve($username);
my $tmpl_view = $self->load_tmpl( "view_user.tmpl" );
$tmpl_view->param( user => $user );
return $tmpl_view->output();
}
=head1 DESCRIPTION
Allows you to use L<HTML::Template::Compiled> as a seamless replacement
for L<HTML::Template>.
=head1 DEFAULT PARAMETERS
By default, the HTCompiled plugin will automatically add a parameter 'c' to the template that
will return to your CGI::Application object $self. This allows you to access any
methods in your CGI::Application module that you could normally call on $self
from within your template. This allows for some powerful actions in your templates.
For example, your templates will be able to access query parameters, or if you use
the CGI::Application::Plugin::Session module, you can access session parameters.
<a href="<tmpl_var c.query.self_url>">Reload this page</a>
With this extra flexibility comes some responsibilty as well. It could lead down a
dangerous path if you start making alterations to your object from within the template.
For example you could call c.header_add to add new outgoing headers, but that is something
that should be left in your code, not in your template. Try to limit yourself to
pulling in information into your templates (like the session example above does).
=head2 Extending load_tmpl()
There are times when the basic C<load_tmpl()> functionality just isn't
enough. The easiest way to do this is by replacing or
extending the functionality of L<CGI::Application>'s C<load_tmpl()> method.
This is still possible using the plugin.
The following code snippet illustrates one possible way of achieving this:
sub load_tmpl
{
my ($self, $tmpl_file, @extra_params) = @_;
push @extra_params, "cache", "1";
return $self->SUPER::load_tmpl($tmpl_file, @extra_params);
}
=head1 FUNCTIONS
This is documentation of how it is done internally. If you actually are looking
for how to use this module, see SYNOPSIS. There isn't anything else to do than
using this plugin.
=head2 import()
Will be called when your Module uses L<HTML::Template::Compiled>. Registers
callbacks at the C<init> and the C<load_tmpl> stages. This is how the plugin
mechanism works.
=cut
sub import {
my $caller = scalar( caller );
# -- determine if the module has been used as base class or as plugin.
return unless $caller->isa('CGI::Application');
$caller->add_callback( 'init' => \&_add_init );
$caller->add_callback( 'load_tmpl' => \&_pass_in_self );
goto &Exporter::import;
} # /import
=head2 _pass_in_self()
Adds the parameter c each template that will be processed. See
DEFAULT PARAMETERS for more information.
=cut
sub _pass_in_self {
my ( $self, $one, $tmpl_params, $template_file ) = @_;
# we won't warn, assuming that the user set param "c" intensionally
#warn("Template param 'c' will be overwritten.") if exists $tmpl_params->{c};
$tmpl_params->{c} = $self;
} # /_pass_in_self
=head2 _add_init()
Set html_tmpl_class to L<HTML::Template::Compiled> at the init stage. That way,
each time a template is loaded using load_tmpl, an instance of
HTML::Template::Compiled will be created instead of the defualt HTML::Template.
See the L<CGI::Appliaction> manpage for more information.
=cut
sub _add_init {
my $self = shift;
$self->html_tmpl_class('HTML::Template::Compiled');
} # /_add_init
=head2 load_tmpl()
This method exists to ensure backward compatibility only. It overrides
CGI::Application's load_tmpl() when this plugin is used the old way.
See BACKWARD COMPATIBILITY for more information and please just don't use it
that way anymore.
( run in 0.624 second using v1.01-cache-2.11-cpan-39bf76dae61 )