white-labelling [Catalyst]
Jonathan Rockway
jon at jrock.us
Thu Jan 24 16:12:56 GMT 2008
"James R. Leu" <jleu at mindspring.com> writes:
> I implemented this in Catalyst by modifying the View:TT path based on the
> contents of $c->request->uri->host(). I'm sure this isn't revolutionary,
> but for the sake of completeness and peer review ....
>
> Here is a trimmed down version of my MyApp::Controller::Root::auto()
>
> sub auto : Private {
> my ($self, $c) = @_;
>
> my $hostMap = {
> 'foo.example.com' => [ 'foo/lib', 'foo/src' ]
> 'bar.example.com' => [ 'bar/lib', 'bar/src' ]
> };
>
> my $server = $c->request->uri->host();
> if (defined($hostMap->{$server})) {
> my $path = [];
> foreach my $elem (@{$hostMap->{$server}}) {
> push(@{$path}, $c->config->{root} . '/' . $elem);
> }
> $c->stash->{additional_template_paths} = $path;
> }
>
> ...
> }
This is nice, but I think I would have implemented this as 3 views:
package View::Foo;
use base 'TT'; # blah blah blah
__PACKAGE__->config( INCLUDE => <the foo files> );
1;
package View::Bar;
use base 'TT'; # blah blah blah
__PACKAGE__->config( INCLUDE => <the bar files> );
1;
package View::TT;
my %view_for = (
'foo.com' => sub { shift->view('Foo') },
'bar.com' => sub { shift->view('Bar') },
);
sub ACCEPT_CONTEXT {
my ($self, $c) = @_;
return ($view_for{$c->req->host} || die 'not found')->($c);
}
1;
Then in your app, you just say:
$c->view('TT')
and you get the correct view automatically.
Code is untested, but the concept is.
Regards,
Jonathan Rockway
More information about the Catalyst
mailing list