From 6dbed02b7cc4ec63a1472d60e5d2fe601cb5a609 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Mon, 23 Jun 2014 20:35:30 +0100 Subject: [PATCH] properly search the template path!! [#103] Conflicts: Netdisco/lib/App/Netdisco/Web.pm Netdisco/lib/App/Netdisco/Web/Plugin.pm --- Netdisco/Changes | 1 + Netdisco/lib/App/Netdisco/Web.pm | 4 ++++ Netdisco/lib/App/Netdisco/Web/Plugin.pm | 7 +++--- .../Template/NetdiscoTemplateToolkit.pm | 23 +++++++++++++++++++ Netdisco/share/config.yml | 4 ++-- 5 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 Netdisco/lib/Dancer/Template/NetdiscoTemplateToolkit.pm diff --git a/Netdisco/Changes b/Netdisco/Changes index 0b5a5a6a..20709f87 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -24,6 +24,7 @@ * Store started timestamp on job completion * Set UTF-8 mode on Pg database connection * Add netdisco-rancid-export to installed scripts + * Template INCLUDE path stuffing broken for plugins [#103] 2.027007 - 2014-05-27 diff --git a/Netdisco/lib/App/Netdisco/Web.pm b/Netdisco/lib/App/Netdisco/Web.pm index dab64938..e238be06 100644 --- a/Netdisco/lib/App/Netdisco/Web.pm +++ b/Netdisco/lib/App/Netdisco/Web.pm @@ -48,6 +48,10 @@ if (setting('extra_web_plugins') and ref [] eq ref setting('extra_web_plugins')) _load_web_plugins( setting('extra_web_plugins') ); } +# after plugins are loaded, add our own template path +push @{ config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} }, + setting('views'); + # workaround for https://github.com/PerlDancer/Dancer/issues/935 hook after_error_render => sub { setting('layout' => 'main') }; diff --git a/Netdisco/lib/App/Netdisco/Web/Plugin.pm b/Netdisco/lib/App/Netdisco/Web/Plugin.pm index e07860a4..adffac6b 100644 --- a/Netdisco/lib/App/Netdisco/Web/Plugin.pm +++ b/Netdisco/lib/App/Netdisco/Web/Plugin.pm @@ -21,7 +21,7 @@ set( ); # this is what Dancer::Template::TemplateToolkit does by default -config->{engines}->{template_toolkit}->{INCLUDE_PATH} ||= [ setting('views') ]; +config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} ||= [ setting('views') ]; register 'register_template_path' => sub { my ($self, $path) = plugin_args(@_); @@ -30,9 +30,8 @@ register 'register_template_path' => sub { return error "bad template path to register_template_paths"; } - unshift - @{ config->{engines}->{template_toolkit}->{INCLUDE_PATH} }, - $path, dir($path, 'views')->stringify; + push @{ config->{engines}->{netdisco_template_toolkit}->{INCLUDE_PATH} }, + dir($path, 'views')->stringify; }; sub _register_include { diff --git a/Netdisco/lib/Dancer/Template/NetdiscoTemplateToolkit.pm b/Netdisco/lib/Dancer/Template/NetdiscoTemplateToolkit.pm new file mode 100644 index 00000000..5a3e31bc --- /dev/null +++ b/Netdisco/lib/Dancer/Template/NetdiscoTemplateToolkit.pm @@ -0,0 +1,23 @@ +package Dancer::Template::NetdiscoTemplateToolkit; + +use strict; +use warnings; + +use Dancer::FileUtils 'path'; +use base 'Dancer::Template::TemplateToolkit'; + +sub view { + my ($self, $view) = @_; + + foreach my $path (@{ $self->config->{INCLUDE_PATH} }) { + foreach my $template ($self->_template_name($view)) { + my $view_path = path($path, $template); + return $view_path if -f $view_path; + } + } + + # No matching view path was found + return; +} + +1; diff --git a/Netdisco/share/config.yml b/Netdisco/share/config.yml index 3c195394..e21eee9d 100644 --- a/Netdisco/share/config.yml +++ b/Netdisco/share/config.yml @@ -219,7 +219,7 @@ warnings: false show_errors: false logger: 'console' engines: - template_toolkit: + netdisco_template_toolkit: encoding: 'utf8' start_tag: '[%' end_tag: '%]' @@ -235,7 +235,7 @@ plugins: provider: 'App::Netdisco::Web::Auth::Provider::DBIC' schema_name: 'netdisco' session: 'YAML' -template: 'template_toolkit' +template: 'netdisco_template_toolkit' route_cache: true appname: 'Netdisco' behind_proxy: false