[Catalyst] Displaying template files without adding new controller actions

Ben van Staveren benvanstaveren at gmail.com
Mon Oct 11 15:14:38 GMT 2010

Hi Anthony,

If you set up your webserver to not send any URL starting with '/static' 
to the Catalyst app, it's easy enough to just have static pages. Since 
you already know what data you are going to put in there, you can easily 
enough build an off-line script that runs as a cronjob and rebuilds the 
pages once every so often; or just add a function to your app that will 
rebuild things every now and then.

The other way to do it (and this is hackish), would be something like this:

sub default :Path {
  my $self = shift;
  my $c = shift;
  my $req_uri = $c->res->path;
  $req_uri =~ s/\/$//;
  # assume that you want the path bit + .tt2 on the end
   my $template = $req_uri . ".tt2";
   if(-e $c->path_to('root','templates',$template)) {
     $c->stash->{template} = $template;
   } else {
     # the template doesn't exist
     $c->stash->{template} = '404.tt2';

This is, however, very non-optimal since you are hitting the filesystem 
for every request that wasn't satisfied by an existing action. Like I 
said, you can either let the webserver serve up static pages. If you are 
worried about how the URL looks in the browser, some mod_rewrite magic 
will fix that for you, but that's a subject for another list :)

YMMV, #include <std_disclaimer,h>, $self->keep_both_pieces if($broken), 
etc. etc. caveat emptor :)


Anthony Gladdish wrote:
>> -----Original Message-----
>> From: Denny [mailto:2010 at denny.me]
>> Sent: 11 October 2010 15:22
>> To: The elegant MVC web framework
>> Subject: Re: [Catalyst] Displaying template files without adding new controller
>> actions
>> On Mon, 2010-10-11 at 15:02 +0100, Anthony Gladdish wrote:
>>> I'd like to add various web pages to my Catalyst app without the need
>>> to modify controllers and restarting the server.
>>> Instead, I just want to add a Template::Toolkit .tt2 file on the file
>>> system and it get picked up automatically.
>> That said, would you mind telling us a bit more about what you're
>> actually trying to achieve in your application?  There may be a better
>> or more standard way of doing it.
> I want to add quite a few .tt2 pages to the app, in various directories.
> I don't want to have to create a Controller action per added template page ( this is the only way I know of adding a web page to a Catalyst app ).
> Every time I add a new page I'll have to add a new action to represent that page then restart the server. There must be a way to add as many template pages as required, and have a single Controller action display the web page if it's corresponding template exists, otherwise 404.
> /app/example/one
> /app/example/two
> /app/example/three/a
> /app/example/three/b
> ... each of these urls will map to a respective .tt2 template which the app will just forward to if it exists.
> Suppose I had a 100 different ( fairly static content ) web pages - I really don't want to have to create an action for each.
> Hope this helps.
> Anthony
> ------------------------------------------------------------------------
> _______________________________________________
> List: Catalyst at lists.scsys.co.uk
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst@lists.scsys.co.uk/
> Dev site: http://dev.catalyst.perl.org/

Ben van Staveren
phone: +62 81 70777529
email: benvanstaveren at gmail.com

More information about the Catalyst mailing list