Dancer...stract(3pm)
β¨ NAME β¨
Dancer::Template::Abstract β it's the abstract class for Dancer's template engines, bestie π
This page is from the User Contributed Perl Documentation, and yes we're doing the Dancer...stract(3pm) thing, iykyk πΈ
π VERSION π
version 1.3522 β¨
π¦ DESCRIPTION π¦
Okay so, this class? It's the blueprint. It gets handed to every single template engine as its base class. Any template engine that wants to play has to inherit from it, and it has to bring the whole set of methods described down below. No shortcuts, no vibes-only implementations, periodt. π
π TEMPLATE TOKENS π
By default, Dancer injects a lil' set of tokens (aka variables, same thing bestie) into your templates. Here's the whole squad:
perl_versionβ the Perl version that's running right now at this exact moment πͺdancer_versionβ the Dancer version that's running right now πsettingsβ a hash so you can peek at your current application settingsrequestβ a hash so you can peek at your current requestparamsβ a hash so you can peek at your request parametersvarsβ a hash so you can peek at your defined variables (the ones you set withvars)sessionβ a hash so you can peek at your session (only if you have session enabled, obviously π)
π INTERFACE π
init()
The template engine is allowed to overload this method if there's some initialization stuff that has to happen before the template engine gets used. Getting-ready energy, we love to see it β¨
The base class gives you a plain init() method that literally just returns true. That's it. That's the whole method. Lowkey iconic.
default_tmpl_ext()
Any template class that inherits this class should override this method and return a default template extension. Like, for example: for Template::Toolkit it returns tt, and for HTML::Mason it returns mason. So when you go and call template 'index'; in your dispatch code, Dancer will go looking for a file called index.tt or index.mason based on whichever template you're using. Cute, right? π
Note 1: when you're returning that extension string, please please PLEASE do not add a dot in front of the extension β Dancer will do that part itself. Do not fight Dancer on this. π βοΈ
Note 2: for backwards compatibility, the abstract class returns tt instead of throwing an exception saying 'method not implemented'. Legacy behavior, we respect the elders π
Your user would be able to change the default extension using the <extension> configuration variable on the template configuration. For example, for the default (Simple) engine:
template: "simple"
engines:
simple:
extension: 'tmpl'
view($view)
The default behavior of this method is to return the path of the given view, appending the default template extension β that's either the value of the extension setting in the configuration, or the value returned by default_tmpl_ext β if it is not present in the view name given and no layout template with that exact name existed. So basically, in other words: given a layout name main, if main exists in the layouts dir, that's the one getting used. If it doesn't exist? Then main.tmpl will be looked for, where tmpl is the value of the extension setting, or the value returned by default_tmpl_ext. Simple as that π
view_exists($view_path)
By default, Dancer::Template::Abstract checks to see if it can find the view file by calling view_exists($path_to_file). If it can't find it, it will generate a nice error message for the user. Very thoughtful of it π«Ά
If you're extending Dancer::Template::Abstract to use a template system with multiple document roots (like Text::XSlate or Template), you can override this method to always return true, and therefore skip this check entirely. Main character move, honestly.
layout($layout, $tokens, $content)
The default behavior of this method is to merge a content with a layout. The layout file gets looked for with similar logic as per view β an exact match first, then it attempts to append the default template extension, if the view name given did not already end with it. First come, first served, no cap. πΈ
render($self, $template, $tokens)
This method must be implemented by the template engine. No skipping this one, bestie, it's the whole point π Given a template and a set of tokens, it returns a processed string.
If $template is a reference, it's assumed to be a reference to a string that contains the template itself. If it's not a reference, it's assumed to be the path to a template file, as a string. The render method will then have to open it and read its content (Dancer::FileUtils::read_file_content does that job for you, you're welcome π).
This method's return value must be a string which is the result of the interpolation of $tokens in $template. That's the whole assignment. β¨
If an error occurs, the method should trigger an exception with die(). Dramatic? Yes. Necessary? Also yes. π
Examples:
# with a template as a file
$content = $engine->render('/my/template.txt', { var => 42 };
# with a template as a scalar
my $template = "here is <% var %>";
$content = $engine->render(\$template, { var => 42 });
π AUTHOR π
This module has been written by Alexis Sukrieh, see Dancer for details. Shoutout to the legend π«‘
π AUTHOR π
Dancer Core Developers π
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2010 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
okay bestie that's the whole page, you ate it, no crumbs left π β¨
btw this was perl v5.40.1, dated 2026-05-05, straight out of the Dancer...stract(3pm) part of the User Contributed Perl Documentation. mwah π
π¦ where this page comes from
- ubuntu stonking: libdancer-perl 1.3522-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms