Welcome to End Point’s blog

Ongoing observations by End Point people

Filling in header elements with Dancer and Template::Flute

Inserting content into Dancer output files involves using a templating system. One such is Template::Flute. In its simplest possible explanation, it takes a Perl hash, an XML file, and an HTML template, and produces a finished HTML page.
For a project involving Dancer and Template::Flute, I needed a way to prepare each web page with its own set of JavaScript and CSS files. One way is to construct separate layout files for each different combination of .js and .css, but I figured there had to be a better way.
Here's what I came up with: I use one layout for all my typical pages, and within the header, I have:
The trick here is, there's no such files "additional.css" and "additional.js". Instead, those are placeholders for the actual CSS and JS files I want to link into each HTML file.
My Perl object has these fields (in addition to the other content):
    $context->{additional_styles}    = [
        { url => '/css/checkout.css' },
        { url => '/css/colorbox.css' },
    $context->{additional_scripts}   = [
        { url => '/javascripts/sprintf.js' },
        { url => '/javascripts/toCurrency.js' },
        { url => '/javascripts/jquery.colorbox-min.js' },
        { url => '/javascripts/checkout.js' },
while my XML file looks like this:



So we have all the elements, but unless you have used all this before, you may not realize how we get the output. (Skip to the punchline if that's not true.)
The XML file is a connector that tells Template::Flute how to mix the Perl hash into the HTML template. Usually you connect things via class names, so in the case of:

the class name in the HTML and the name field in the XML connect, while the iterator field in the XML and the hash key in the Perl hashref do as well. The case of a
means that the hash value must be an arrayref of hashrefs, i.e.,
  "additional_style" => [
    { url => "...", },
Important note: if the hash value is undefined, you'll get a run-time error when you try to expand the HTML template, and if you have an empty arrayref, the result of the expansion is an empty string (which is just what you want).
And so, through the magic of Template::Flute, what the browser sees is:


No comments: