[cgiapp] Re: CGI::Application::Plugin::HTCompiled update: need some help

Alex capfan at gmx.de
Sat Aug 1 04:39:52 EDT 2009


Here is the new code. I'm unsure if the POD is the way it should be, as I
documented the use of import and the methods added as callbacks. Please feel
free to change anything :)

Changes:
- requires CA v4.31
- does not override load_tmpl anymore
- uses html_tmpl_class to switch to HT::Compiled
- warns if there already is a c-param.

Hopefully, I don't have to turn this into a git pull request, as I don't get
along with git atm. It would take a while...

Best regards, Alex

[code]
package CGI::Application::Plugin::HTCompiled;

use CGI::Application 4.31;

use 5.006;
use strict;
use warnings;
use Carp;
use Data::Dumper qw/Dumper/; # dev only

=head1 NAME

CGI::Application::Plugin::HTCompiled - Integrate with
HTML::Template::Compiled

=cut

$CGI::Application::Plugin::HTCompiled::VERSION = '1.02';

use vars qw( $VERSION );


=head1 SYNOPSIS

    # In your CGI::Application-derived base class. . . 
    use base "CGI::Application";
	
	use CGI::Application::Plugin::HTCompiled;

    # Later, in a run mode far, far away. . . 
    sub view
    {
        my $self = shift;
        my $username = $self->query->param("user");
        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 HTML::Template, as far as is possible with that module. 




=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).




=head1 FUNCTIONS

=head2 import()

Will be called when your Module uses L<HTML::Template::Compiled>. Registers
callbacks at the inti and the load_tmpl stages.

=cut

sub import {
    my $caller = scalar( caller );
	$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 ) = @_;
	
	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 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 AUTHOR

Mark Stosberg C<< <mark at summersault.com> >>
...but largely modeled on HTDot plugin by Jason A. Crome. 

=head1 BUGS

Please report any bugs or feature requests to
C<bug-cgi-application-plugin-htcompiled at rt.cpan.org>, or through the web
interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=CGI-Application-Plugin-HTCo
mpiled>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.

=head1 ACKNOWLEDGEMENTS

The usual crowd in #cgiapp on irc.perl.org 

=head1 SEE ALSO

L<CGI::Application>, L<HTML::Template>, L<HTML::Template::Compiled>,

=head1 COPYRIGHT & LICENSE

Copyright 2005 Mark Stosberg, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.

=cut

1; # End of CGI::Application::Plugin::HTCompiled
[/code]

-----Original Message-----
From: cgiapp-bounces at lists.openlib.org
[mailto:cgiapp-bounces at lists.openlib.org] On Behalf Of Mark Stosberg
Sent: Freitag, 31. Juli 2009 22:51
To: cgiapp at lists.openlib.org
Subject: [cgiapp] Re: CGI::Application::Plugin::HTCompiled update: need some
help

On Thu, 23 Jul 2009 12:37:37 -0400
Mark Stosberg <mark at summersault.com> wrote:

> 
> > sub html_tmpl_class { 'HTML::Template::Compiled' }
> > 
> > But: how do I replace the html_tmpl_class() in the calling app? I 
> > don't get how I call the init hook. I guess (at least I hope) it's 
> > something very simple anyone of you can show me J
> 
> Alex,
> 
> I'll release a new version of CGI::App soon that will make that easy. 
> I have already previewed the version here: It allows setting 
> html_tmpl_class as an object method. So, you'll be able to use the 
> "init" callback, and do this at the 'init' stage:
> 
>   $self->html_tmpl_class('HTML::Template::Compiled');
> 
> I'll try to get that release made soon.

That's done now:

http://search.cpan.org/~markstos/CGI-Application-4.31/

    Mark



#####  CGI::Application community mailing list  ################
##                                                            ##
##  To unsubscribe, or change your message delivery options,  ##
##  visit:  http://lists.openlib.org/mailman/listinfo/cgiapp    ##
##                                                            ##
##  Web archive:   http://lists.openlib.org/pipermail/cgiapp/   ##
##  Wiki:          http://cgiapp.erlbaum.net/                 ##
##                                                            ##
################################################################
Eingehende eMail ist virenfrei.
Von AVG überprüft - www.avg.de
Version: 8.5.392 / Virendatenbank: 270.13.38/2274 - Ausgabedatum: 07/31/09
05:58:00 



More information about the cgiapp mailing list