Recommended way to build URLs in Template Toolkit

On the mailing list, Andy Wardey recommended using the URL plugin as the best way to build URLs in Template Toolkit (TT). Since this is very helpful but sometimes hard to dig out of the TT documentation, I wanted to make a quick note here.

The URL plugin will make sure to properly HTML and URI encode your link(s).

Here my quick example

In your template:
[% USE profile = URL( '/profile', show_picture = 1 ) %]

<a href="[% profile( slayer = 'buffy' ) %]">Buffy's Profile</a>
<a href="[% profile( vampire = 'spike' ) %]">Spike's Profile</a>
This will render validly encoded html like:
<a href="/profile?show_picture=1&amp;slayer=buffy">Buffy's Profile</a>
<a href="/profile?show_picture=1&amp;vampire=spike">Spike's Profile</a>
Notice the & not a '&' which makes the html validation much happier.

This is much easier than trying to do it yourself with filters and/or plugins and/or custom url encoding code (please save us :)

Comments

Popular posts from this blog

Changing Dancer::Plugin::Ajax's content type

BootstrapX clickover

2 ways to get SQLite to put dates into columns