I write this post to keep notes of those little knowledge snippets when I am learning Dancer, the Perl web framework.
Templates all go into the views/ directory. Optionally, you can create a “layout” template which provides a consistent look and feel for all of your views.
If we have some code like “‘msg’ => get_flash()”, this means we have a msg field which displays a message to the user when an event happens like a new entry is posted, or the user logs in or out. It’s called a “flash” message because we only want to display it one time, not every time the / URL is rendered.
The uri_for directive tells Dancer to provide a URI for that specific route. You might ask why we don’t simply hardcode the /add URI in our application or templates. The best reason not to do that is because it removes a layer of flexibility on where to “mount” the web application. Although the application is coded to use the root URL / it might be better in the future to locate it under its own URL route – at that point we’d have to go through our application and the templates and update the URLs and hope we didn’t miss any of them. By using the uri_for Dancer method, we can easily load the application wherever we like and not have to modify the application at all.
We will use the format like “insert into entries (title, text) values (?, ?)” to be sure our application won’t be vulnerable to SQL injection.
If we have some like “any [‘get’, ‘post’] => ‘/login’ => sub {}”, here a GET for a human browsing to the URL and a POST for the browser to submit the user’s input to the web application.
session->destroy; is Dancer’s way to remove a stored session.
If we have some code like “set layout => ‘main’”, what this tells Dancer’s template engine is that it should look for a file called main.tt in myapp/views/layouts/ and insert the calls from the template directive into a template parameter called content.
The “before_template_render” provides a way to manipulate the template parameters before they’re passed to the engine for processing.