Skip to content

Perl web development – Step 5: First Changes

This is step 5 in the Perl web development series. In this step we make our first changes to our Dancer2 app.

After spending 4 steps in setting up of our environment it’s finally time to make changes and see what happens. After we’ve made the changes and approve of those we commit them to the git repository to remain in the version control as our first real change commit.

Alter template tags

The tags used to get Dancer2 variables in the ‘simple’ Dancer2 templating system look like ‘ <% var_name %>‘. In step 3 of the series we’ve installed the Template module via Carton and as a result we have Template Toolkit available. Template Toolkit has it’s own variation of writing variables that looks like ‘[% var_name %]‘. If you’re fine with using the simple templating system skip this next part. If you want to keep a little more close to Template Toolkit I suggest altering the tags.

Start with changing the settings of your config file. Open up your config.yml file located in the app directory. If you’ve followed the previous steps you should find it here: ‘/Users/***/Dev/MovieCollection/MovieCollection/config.yml‘. Remember, we’re working with YAML files here, so keep the indentation intact or it will fail.

In this file locate the template section:


# template engine
# simple: default and very basic template engine
# template_toolkit: TT
template: "simple"
# template: "template_toolkit"
# engines:
#   template:
#     template_toolkit:
#       start_tag: '<%'
#       end_tag: '%>'

Comment / Remove the ‘template: “simple”‘ line and uncomment the # template: “template_toolkit” line.
Now Dancer2 knows we are going to use Template Toolkit as our templating system. Next, setup the Template Toolkit config. As mentioned earlier I like to keep the original Template Toolkit syntax so replace the ‘engines’ part with this:


engines:
template:
template_toolkit:
start_tag: '[%'
end_tag: '%]'

You have now uncommented the engines section and setup the new variable tags, meaning Dancer2 is now ready to go using Template Toolkit as it’s templating engine.

Change Dancer2 variabele tags

In order to test our new settings, we have to change the Dancer2 variables to use our newly adjusted tags and restart the app to see if everything works. The variables exists in both the layout file as the index file. All of these files can be found in the ‘views‘ directory.

Open ‘/Users/***/Dev/MovieCollection/MovieCollection/views/layouts/main.tt‘.
In this file you can find a couple of Dancer2 specific variables in the old notation style. Alter these to have the new notation style. So for every <% var_name %> change it to [% var_name %]. As an example, change <% content %> to [% content %]. Do this for all the variables in the the layout file and in ‘/Users/***/Dev/MovieCollection/MovieCollection/views/index.tt‘.

At this point of the app you could go for a quick search and replace or use a Perl one-liner. Whatever makes your cookie crumble.

Restart the app

After making the changes we need to test if the app still works and works as expected. On your running console hit ‘Control + C‘ to close the webserver execution. After that, run

carton exec plackup -p 5000 MovieCollection/bin/app.psgi

to start the webserver again. Open up localhost:5000 in your browser and you should see the result immediately. In fact, you wouldn’t notice a difference… if you do… something went wrong, and that’s were the debugging starts 🙂

Any problems? Please feel free to leave a comment.

Published inCode