This can be done in two slightly different ways:
First by adding a before_render
hook and setting a variable. It's easy to pack it all inside a plugin like so:
package Mojolicious::Plugin::TemplateName;
use Mojo::Base 'Mojolicious::Plugin';
sub register {
my ($self, $app, $conf) = @_;
$app->helper('template' => sub { return shift->stash('mojo.template') });
$app->hook(before_render => sub {
my $c = shift;
$c->stash('mojo.template', $_[0]->{template} )
});
}
1;
and use it inside a template like this
<%= template %>
Second, it can be done inside the templates - by setting the variable inside the template itself:
% stash('template', __FILE__);
and then reusing the variable in the layout:
<%= $template %>
In this case you get the file name with suffix and all - not just the template.
Inspired by the answer here about templates being rendered inside-out.
<%= __FILE__ %>
. – Expertize