[Catalyst] Help System - Can a controller's index capture the remainder of the path as a series of arguments?

Octavian Rasnita orasnita at gmail.com
Thu Mar 31 17:31:27 GMT 2011


From: "Kyle Hall" <kyle.m.hall at gmail.com>
> Hello All,
> 
> I'm trying to a help system to my catalyst app. My idea is to have a
> help button. If we are currently at /foo/bar, the help buttons uri
> becomes /help/foo/bar. My idea is that the the Help controllers index
> will capture everything after /help ( hopefully as an array ). This
> data will then be used to look up and display the correct help file (
> just a tt template ). So, if the help button is currently pointing to
> /help/foor/bar, the system will display the template located at
> /help/foo/bar.tt.
> 
> Can this be done? Is there a much better way to handle this that I
> have not seen or thought of?
> 
> Thanks,
> Kyle


Hi Kyle,

In the templates where you will display the link to the Help file, you can use:

<a href="[% c.uri_for_action('/help/index', c.req.path).path %]">Help</a>

Then in the Help controller's index action you can use:

sub index :Path :Args(1) {
    my ( $self, $c, $path ) = @_;
    $c->stash( template => "help/$path.tt" );
}

And you will need to add the templates with help content under a "help" directory.
Of course, if you want you can avoid using a centralised "help" directory, and store the templates with help in the directory  with the other templates you want to offer help to. In that case, you can use:

    $c->stash( template => "$path-help.tt" );

... or something like that.

It would be pretty simple, but this style is not very reliable nor recommended because there may be more urls for accessing the same content, and in that case you would need to create a template for each one, or there may be necessary to add more than one "Help" link on a page for a certain part of it.

Unless there is a specific reason you want to keep the help content in static text files, it would be better if you'd keep it in a DB table, and access the wanted help using a label or just a simple ID:

<a href="[% c.uri_for_action('/help', 'your-label-here').path %]">Help</a>

It would be much more search engine friendly, and this way you will be able to offer the help in more languages without using Catalyst's I18N for this.

sub index :Path :Args(1) {
    my ( $self, $c, $id ) = @_;
    my $help = $c->model( 'DB::Help' )->find( $id );
    $c->stash( help => $help );
}

Octavian




More information about the Catalyst mailing list