Dancer and TemplateToolkit

I was struggling with an issue with my Dancer work. I wrote code to pull a collection of rows from a database table and pass them into a template file for display. The template file would then iterate through each row in the collection and display the data in a new HTML table row. When I tested this out, it was not working.

nn

It turns out that the default template engine that Dancer uses (Dancer::Template::Simple) does not support parsing advanced data structures. It cannot handle listrefs or hashrefs passed to it from a Dancer controller. In order to use these data structures, you need to use a more advanced template engine, like Template Toolkit.

nn

I don’t know much about Template Toolkit. It appears to be an independent template engine that many web frameworks in many different languages supports. The website with more information is here.

nn

Here is how to use Template Toolkit in your Dancer environment. These instructions assume that you have not installed Template Toolkit before for this environment.

nn

Install Template modules from CPAN

nn

cpanm Templaten

nn

Change all <{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}> to [{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}] in your template files

nn

If you were using the simple template, all the variables in your template files will be surrounded by <{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} and {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}> characters. Template Toolkit does not recognize these characters out of the box, so you must change them to the default [{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} and {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]. I believe that you can change this in the Dancer configuration, but maybe just stick with the defaults to make life easy. This action can be performed with any decent text editor; use the global search and replace function.

nn

Update Dancer’s YAML configuration

nn

Open your config.ym file. Find and comment out the line that says template:simple. Insert the lines to enable Template Toolkit. The file should contain this now:

nn

# template: "simple"ntemplate: "template_toolkit"nengines:n    template_toolkit:n        start_tag: '[{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}'n        end_tag:   '{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]'n

nn

Validate how you pass data from your controller to your template

nn

Simple data elements, like a hash or list, can be passed by value.

nn

template 'viewWorker.tt', { worker => $worker };n

nn

Complex data elements, like a list of hashes, must be passed by reference (place a in front of it).

nn

# @workerList is a list of hashesntemplate 'viewWorkers.tt', { workerList => @workerList };n

nn

Access data in your templates

nn

To iterate through a complex data structure, like a list of hashes, use the FOREACH function. You can then access the data fields using dot notation.

nn

[{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} FOREACH worker IN workerList {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]    n   <tr>n        <td>[{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} worker.workerId {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]</td>n        <td>[{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} worker.firstName {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]</td>n        <td>[{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} worker.lastName {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]</td>n        <td>Nil</td>n    <td><a href="/viewWorker.tt?workerId=?[{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} worker.workerId {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]">View</a></td>n    </tr>n    [{ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad} END {ae83d9d521226808aaf5363be37edd826578aa3462b1edb98735bf6151518dad}]n

Leave a Reply

Your email address will not be published. Required fields are marked *