I'm learning Perl and using Dancer as a web framework. I've got two views (tt files) which should share the same navigation. So, it would be great to start learning how to manage templates for navigation and footers.
I've read the documentation for the Template Toolkit and I've done the following:
I've changed the config.yml
file to:
#template: "simple"
template: "template_toolkit"
engines:
template_toolkit:
start_tag: '[%'
end_tag: '%]'
I've defined the templates in the .pm
file:
package proyecto;
use Dancer ':syntax';
our $VERSION = '0.1';
get '/' => sub {
template 'index';
};
get '/menu' => sub {
template 'menu';
};
true;
There is a link in the index template directing the visitor to the menu template:
<li class="active">< a href="/menu">Menu <span class="sr-only"></span></a></li>
I would like to reuse the navigation code from index.tt into menu.tt, so I've wrapped the navigation code in index.tt with the following:
[% BLOCK navigation %]
#my nav code
[% END %]
To finally include that code in the menu.tt file, I've written the following (where the navigation code should be):
[% navigation = 'index.tt' %]
[% INCLUDE navigation %]
The files index.tt
and menu.tt
are located in the folder views. But it seems it's not that easy! =( Any suggestion on how to reuse code from one file to another which is located in the same directory?