Question

I'm building a Catalyst application and I want to use Template Toolkit as my templating system. However, my application will be responding to many domains, and depending on the domain, it will display different content with different Template Toolkit configs. Essentially, if I have domain.com and domain2.com, I'd like each to have its own config. Does anyone know of an elegant way to do this while still being able to use Catalyst's view feature? Thanks!

UPDATE

By putting this in the Root Controller, I've figured out that I can change the site wrapper in the config fro the Template Toolkit object. However, it just seems to use the initial wrapper located in lib/myapp/View/HTML.pm (I'm guessing because it's just using the wrapper that it was initialized with). Is there anyway to get it to recognize that I changed the wrapper for this request?

sub begin :Private {
    my ($self, $c) = @_;

    $c->view('HTML')->config->{WRAPPER} = $c->req->uri->host . '/site/wrapper';
}
Was it helpful?

Solution

I tend to declare WRAPPER in each main template directly, rather than globally like that.

[%- WRAPPER c.req.uri.host _ '/site/wrapper.tt'; -%]

... as I find it allows for far more flexibility.


However...

Another option that I have had great success with is using a custom template path, so that domain specific versions of your templates can over-ride defaults, where they exist. This gives you enormous flexibility to inject variations of your code as required.

=== MyApp::View::TT.pm ===

sub process {
    my ($self, $c) = @_;

    # capture original path, identify host
    my @orig_include_path = @{$self->include_path};
    my $domain = $c->req->uri->host;

    # augment every path with domain/path
    @{$self->include_path} = map { ("$domain/$_", $_) } @orig_include_path;

    $self->SUPER::process($c);

    # revert path
    @{$self->include_path} = @orig_include_path;
}

The net result of which is that every call for a WRAPPER, a PROCESS or an INCLUDE will check for a domain specific version or produce the generic version, i.e.

[%- WRAPPER wrapper.tt -%]

will find $domain/wrapper.tt or fall back to wrapper.tt if there's no such file. This automatically extends to:

  • foo/bar/baz.tt
  • domain1/foo/bar/baz.tt
  • domain2/foo/bar/baz.tt

Hope that's useful.

OTHER TIPS

Just historically the best idea might be something like: https://metacpan.org/release/Catalyst-TraitFor-Component-ConfigPerSite which provides the ability to specify configuration per site/host. Thus you can specify separate view/wrapper/db connection and etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top