Search code examples
perlpluginsdancer

Dancer plugin loading a template


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;

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);