Question

How can I load a template from a Dancer::Plugin which is not in 'app/views' directory without changing views default directory?

This isn't working /it adds the default views path to the file path/:

package Dancer::Plugin::MyPlugin;
use Dancer ':syntax';
use Dancer::Plugin;

any '/test' => sub {
    template '/path_to_template/test.tt' => {
    };
};

register_plugin;

1;
Was it helpful?

Solution

You could call engine to get the Dancer::Template object and call its render method, e.g.:

my $template_engine = engine 'template';
my $content = $template_engine->render('/path/to/template.tt', { 'name' => 'value' });

Then, to return the rendered content in the default layout, call apply_layout:

return $template_engine->apply_layout($content);

OTHER TIPS

Currently, I think you'd need to set the views setting before the template call, then change it back afterwards, for instance:

my $views_dir = setting('views');       # remember current setting
setting 'views' => '/some/other/path';  # temporarily use our desired path
my $content = template 'test', $params; # render the view
setting 'views' => $views_dir;          # restore previous setting
return $content;

That, however, is ugly.

I think it would make sense for the template keyword to accept a system_path option, much like send_file does, so you could say, e.g.:

template '/path/to/view.tt', $params, { system_path => 1 };

I've raised an issue for this, and will look in to getting it implemented for the next release: https://github.com/sukria/Dancer/issues/645

(Disclosure: I'm part of the Dancer dev team)

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