[cgiapp] [patch] CAP::MessageStack + CAP::FormState

Alex capfan at gmx.de
Tue Oct 20 15:43:00 EDT 2009


Hi!

Considering the multiple parsing of the template: what if I would override
the tmpl_file to the parsed output? Like this (watch the "# ?"):
[code]
sub _pass_in_messages {
    my ( $self, $ht_params, $tmpl_params, $tmpl_file ) = @_;

    # get the proper messages and update $tmpl_params
    my $session = _check_for_session( $self );
    my $current_runmode = $self->get_current_runmode();
    my $message_stack = $session->param( '__CAP_MessageStack_Stack' );
    my $messages = _filter_messages( $message_stack, { -scope =>
$current_runmode }, 1 );
    my $loop_name = $config{'-loop_param_name'} || 'CAP_Messages';

	# -- check for HTML::Template and die_on_bad_params => 1
    if( (!exists $ht_params->{die_on_bad_params} or
$ht_params->{die_on_bad_params} = 1)
	   and $self->html_tmpl_class() eq 'HTML::Template' and scalar(
@$messages ) > 0 ) {
	
        my $t = undef;
        # -- copied from CGI::Application::load_tmpl()
        if( ref $tmpl_file eq 'SCALAR' ) {
            $t = HTML::Template->new( scalarref => $tmpl_file, %{$ht_params}
);
        } elsif ( ref $tmpl_file eq 'GLOB' ) {
            $t = HTML::Template->new( filehandle => $tmpl_file,
%{$ht_params} );
        } else {
            $t = HTML::Template->new( filename => $tmpl_file,
%{$ht_params});
        }
		
		if( $t->query(name => $loop_name) ) {
			$tmpl_params->{ $loop_name } = $messages if scalar(
@$messages );
			$self->clear_messages( -scope => $current_runmode )
if ( $config{'-automatic_clearing'} );
			# -- im'm not sure, but would it make sende to do:
			$tmpl_file = \$t->output(); # ?
		}

    }else{
		# -- pass in anyways
		$tmpl_params->{ $loop_name } = $messages if scalar(
@$messages );
		$self->clear_messages( -scope => $current_runmode ) if (
$config{'-automatic_clearing'} );
	}

}
[/code]

Regards, Alex

-----Ursprüngliche Nachricht-----
Von: cgiapp-bounces at lists.openlib.org
[mailto:cgiapp-bounces at lists.openlib.org] Im Auftrag von Michael Graham
Gesendet: Dienstag, 20. Oktober 2009 16:30
An: cgiapp at lists.openlib.org
Betreff: Re: [cgiapp] [patch] CAP::MessageStack + CAP::FormState


> And yes, die_on_bad_params is not good for looking for HTML:T:emplate, 
> you're right. We first have to look for it via tmpl_class (which, if 
> always setting is our consense), and then evaluate it. The later check 
> for die_on_bad_params is only a performance issue, as we don't need 
> the check if we don't care about template variable declaratoion.

I'm not worried about a performance issue regarding checking a single hash
value.  That's so tiny that it's not worth worrying about.

However since you bring up performance issues, I will reiterate Jason's
point that if you do have die_on_bad_params set, then every template you
have will be loaded and parsed twice.  In fact, if you are using FormState
and MessageStack together, then every template will be loaded and parsed
*three* times.  THAT is a performance issue.  However, since it only affects
people using die_on_bad_params, I'm not going to get too worked up about it.


> Btw, HTML::Template::Compiled is HTML::Template derived but exactly 
> not the same (e.g. die_on_bad_params and associate is not
> implemented) and should therefore set up another tmpl_class.

The method I'm using for determining whether the templating system supports
the HTML::Template die_on_bad_params feature is by using 'isa':

    sub _template_class_isa_html_template {
        my $self = shift;
        # first check to see if the template class is HTML::Template or one
of its
        # relatives, e.g. HTML::Template::Pluggable, HTML::Template::Expr
        
        my $html_tmpl_class = $self->html_tmpl_class;
        if ($html_tmpl_class) {
            eval "require $html_tmpl_class;";
            if (eval { $html_tmpl_class->isa('HTML::Template') }) {
                return 1;
            }
        }
        return;
    }

I think this is the proper way of checking, since it works correctly for all
the HTML::Template related modules I'm aware of:
HTML::Template, HTML::Template::Expr, HTML::Template::Pluggable and
HTML::Template::Compiled.

Note that for HTML::Template::Compiled, it (correctly) returns false,
because HTML::Template::Compiled is not a subclass of HTML::Template.  


Michael


--
Michael Graham <magog at the-wire.com>

#####  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/                 ##
##                                                            ##
################################################################



More information about the cgiapp mailing list