Having multiple INCLUDE_PATH for the Template Toolkit engine
HI, The Perl Maven and the Code Maven sites run using the same code-base and thus the same set of templates in the views directory. https://github.com/szabgab/Perl-Maven I'd like to have some additional templates that differ in the two sites, but I don't want to have two separate sets of templates. So I wanted to add multiple views directories which worked in the config file like this engines: template: template_toolkit: encoding: 'utf8' INCLUDE_PATH: - 'views' - 'config/templates' start_tag: '<%' end_tag: '%>' but I'd like to set this during the run-time, for example in the 'before' hook. So far I could not do it. I tried set views => [dir1, dir1] but does not work as views is required to be a string. I could not figure out how else to change the INCLUDE_PATH during run-time. Is there a solution? If currently not,would it be possible to allow views to be either a string or an array ref to strings? regards Gabor
On 10/11/2015 6:06 pm, Gabor Szabo wrote:
Is there a solution? If currently not,would it be possible to allow views to be either a string or an array ref to strings?
Allowing views to be an arrayref of strings is likely to cause issues with other template engines. TT2 allows you to supply coderefs as part of its INCLUDE_PATH configuration. (See Template::Manual::Config for the details). Configuring the template engine within your app may allow you to do what you want: eg ( warning - untested config!! ) set engines => { template => { template_toolkit => INCLUDE_PATH => [ sub { # code that returns absolute paths to your view dirs map { path( app->location, $_) } 'views', app->request->var('other_views'); }], ... } }; set template => 'template_toolkit'; Hope that helps, Russell.
On Tue, Nov 10, 2015 at 3:18 PM, Russell Jenkins < russell.jenkins@strategicdata.com.au> wrote:
On 10/11/2015 6:06 pm, Gabor Szabo wrote:
Is there a solution? If currently not,would it be possible to allow views to be either a string or an array ref to strings?
Allowing views to be an arrayref of strings is likely to cause issues with other template engines.
TT2 allows you to supply coderefs as part of its INCLUDE_PATH configuration. (See Template::Manual::Config for the details). Configuring the template engine within your app may allow you to do what you want: eg ( warning - untested config!! )
set engines => { template => { template_toolkit => INCLUDE_PATH => [ sub { # code that returns absolute paths to your view dirs map { path( app->location, $_) } 'views', app->request->var('other_views'); }], ... } }; set template => 'template_toolkit';
That sounded like a good idea, but unfortunately it throws an exception: Can't call method "params" on an undefined value at .../Dancer2/Core/Role/Template.pm line 180. It happens even on Dancer2 app that I've just created using dancer2 -a where I've removed the template entry from the config.yml and added this to the code: hook before => sub { set engines => { template => { template_toolkit => { start_tag => '<%', end_tag => '%>', } } }; set template => "template_toolkit"; }; Gabor
Opened a ticket with this including a test case. https://github.com/PerlDancer/Dancer2/issues/1059 It would be nice if someone could review it. Gabor On Thu, Nov 12, 2015 at 5:00 PM, Gabor Szabo <gabor@szabgab.com> wrote:
On Tue, Nov 10, 2015 at 3:18 PM, Russell Jenkins < russell.jenkins@strategicdata.com.au> wrote:
On 10/11/2015 6:06 pm, Gabor Szabo wrote:
Is there a solution? If currently not,would it be possible to allow views to be either a string or an array ref to strings?
Allowing views to be an arrayref of strings is likely to cause issues with other template engines.
TT2 allows you to supply coderefs as part of its INCLUDE_PATH configuration. (See Template::Manual::Config for the details). Configuring the template engine within your app may allow you to do what you want: eg ( warning - untested config!! )
set engines => { template => { template_toolkit => INCLUDE_PATH => [ sub { # code that returns absolute paths to your view dirs map { path( app->location, $_) } 'views', app->request->var('other_views'); }], ... } }; set template => 'template_toolkit';
That sounded like a good idea, but unfortunately it throws an exception:
Can't call method "params" on an undefined value at .../Dancer2/Core/Role/Template.pm line 180.
It happens even on Dancer2 app that I've just created using dancer2 -a where I've removed the template entry from the config.yml and added this to the code:
hook before => sub { set engines => { template => { template_toolkit => { start_tag => '<%', end_tag => '%>', } } }; set template => "template_toolkit"; };
Gabor
participants (2)
-
Gabor Szabo -
Russell Jenkins