
stephenclouse at gmail
Jun 9, 2009, 9:21 AM
Post #9 of 11
(1218 views)
Permalink
|
|
Re: Best practice: How to build app parts reusable?
[In reply to]
|
|
On Mon, Jun 8, 2009 at 10:42 PM, Robert Buels <rmb32 [at] cornell> wrote: > Matt S Trout wrote: > >> Just have your controller base class set: >> >> $c->stash(additional_template_paths => $self->template_paths); >> > > Can you do something similar if using Mason? > Yes, use multiple comp_roots: <code> package MyApp::View::Mason; use parent 'Catalyst::View::Mason'; __PACKAGE__->config( comp_root => [ [ foo => '/myapp/root/templates/foo' ], [ bar => '/myapp/root/templates/bar' ], ], ); </code> The comp_roots are treated as a single component tree by the Mason resolver. If the same template path exists in multiple comp_roots, the one in the first listed comp_root is used; the later ones are never seen. All standard Mason mechanics apply to the resulting component tree. This config syntax, however, can be a pain to include in external config files, since it's an array of arrays (YAML looks ugly, Config::General can't even do it at all, the rest I don't know). My hack was to use a COMPONENT hook to transform a single key/value into whatever I needed. Here's an example of one I use with a multiple-client app to setup a client-specific template directory followed by a common template set: <code> sub COMPONENT { my $self = shift; my($c,$arguments) = @_; $arguments->{comp_root} ||= [ [ client => $c->path_to('root/template/client', lc($c->config->{client})) ], [ base => $c->path_to('root/template/base') ], ]; $self->next::method(@_); } </code> So the above with config: <MyApp> client foo </MyApp> results in the equivalent of: <code> MyApp->config( comp_root => [ [ client => '/myapp/root/template/client/foo' ], [ base => '/myapp/root/template/base' ], ], ); </code> -- Stephen Clouse <stephenclouse [at] gmail>
|