[Catalyst] Lightweight error/messages reporting to templates

Nathaniel Nuss ogmoid at gmail.com
Thu May 24 06:27:59 GMT 2007


On Wed, May 23, 2007 at 07:29:57PM +0100, Paul Makepeace wrote:
> I'm hoping there's a 'best practice' or common pattern for reporting
> errors and messages to templates (not necessarily just forms).

I've opted for defining a function in a Controller base class.

<contrived example>


package MyApp::Controller::Base;

sub sysmsg {
    my ( $self, $c, $type, $msg ) = @_;
    # Push [ $type, $msg ] onto the flash to preserve the message across
    # redirect.
    push @ {$c->flash->{sysmsg}}, [ $type, $msg ];
}


package MyApp::Controller::Whatever;

use base 'MyApp::Controller::Base';
...
    if ( $something_noteworthy ) {
        $self->sysmsg( $c, 'err', 'Error. Halt Catch Fire.' );
    }
...
1;


msg template [here TT2 and part of a series of WRAPPERs]

[%- IF c.flash.sysmsg.size %]
[%- FOREACH msg IN c.flash.sysmsg %]
[%- IF msg.0.length %]
<div class="[% msg.shift %]">[% msg.shift %]</div>
[%- END ; #IF %]
[%- END ; #FOREACH %]
[%- END ; #IF %]


</contrived example>

I also implemented breadcrumb dropping similarly.

-- 
Nate Nuss

[ The IF wrapper around the <div> was for an issue I was having (but haven't
yet followed up on) where the same content skeleton exists on the second try
but the content has been consumed (now empty) ]

( [Kind] critique welcomed. )



More information about the Catalyst mailing list