[Catalyst] Displaying template files without adding new controller actions

Stuart Watt swatt at infobal.com
Mon Oct 11 18:34:53 GMT 2010


If it's a TT file you need (i.e., not static), along with appropriate processing, you can make a variation on Ben's suggestion by using the $c->request->arguments, and putting the file name into $c->stash->{template} before handing off to the TT view, which is likely to be the default. This is a slight variation on ben's suggestion, which will only hit the file system when looking at the right part of the URI space. 

I've not tested this, but it'd just look something like:

sub dynamic : Local {
  my ($self, $c) = @_;
  my $args = $c->request->arguments();
  $c->stash->{template} = join('/', @$args);
};

This omits the error handling (assumes TT will take care of that) but should only handle requests that begin /dynamic. The arguments() method returns additional stuff after "/dynamic" in the requesting URL. 

I do something similar in my code (it's not precisely the same, as I retrieve the file contents through a model) and it works pretty well for us. 

For security, you probably want to remove stuff like "..", just to be safe. 

All the best
Stuart


On 2010-10-11, at 11:14 AM, Ben van Staveren wrote:

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

> 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 :)
> 
> ...
> 
> Anthony Gladdish wrote:
>> 
>> 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.
>> 
>> ...
> 
> -- 
> Ben van Staveren
> phone: +62 81 70777529
> email: benvanstaveren at gmail.com

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20101011/fcc75af6/attachment.htm


More information about the Catalyst mailing list