Template Routes in ExpressionEngine 2.8

by: Quinn Chrzan on: 2/26/2014

When developing a website, designing the structure of your URLs and sitemap can be an important consideration. ExpressionEngine by default abstracts away a lot of this and automatically gives you pretty URLs based on your template group and template name. For example if your group name is “blog” and your template name is “entry” your URL will look like:


Most of the time this works well, but occasionally you want fine-grained control over your URLs. ExpressionEngine 2.8 introduces a new feature called Template Routes, which gives you all the control you need.

Template Routes allow you to customize your URLs. You can define rules for each segment of your URL and assign segments to variables that can be used in your templates. Let’s look at an example of how you might use this.

Event Calendar

Suppose you are setting up an event calendar that displays all events for a given date. You might use a Template Route that looks something like this:


The above route will match URLs such as the following:


In our template we can access the URL segments with the traditional syntax {segment_n}, or we can use the variables we defined in our route like so:


For our event calendar’s template we could use a Channel Entries tag along with our segment variables to display the desired entries. Your template would look something like this:

{exp:channel:entries year="{segment:year}" month="{segment:month}" day="{segment:day}"}

Template Routes comes with a variety of rules that can be chained together to make complex rules for your URLs. And if there isn’t a rule that fits you can use your own regular expression. Let’s look at another example.

Product Page

For this example we want to build a product page, in this case our products are identified by a SKU. SKUs are typically between seven and twenty characters long and consist of the characters A through Z and 0 through 9. We can combine several rules in our route to make sure it only matches valid SKUs. In this case we need to define a minimum length, maximum length and restrict the segment to only alphanumeric characters:


If you spend your Sundays playing regex golf instead of real golf you’re probably thinking that you know a better way to match that. Template Routes comes with a rule that allows you to define your own regular expressions. We could rewrite the previous Template Route with a regular expression like so:


In your templates you often need to go the other direction and print out a URL to a particular template. Normally this is accomplished using the {path=... variable, Template Routes uses a new global variable called the {route=... variable. The Route variable looks like this: {route="group_name/template_name" variable="value" .... Just provide the Group Name and Template Name of the template you want to generate a URL for, and optionally provide values for any defined segment variables in your Template Route. Here’s an example of how you would generate a URL for our product page:

{route='products/info' sku='B00032G1S0'}

A common structure for URLs is to drill down from a listings page to an item detail page. In the case of our products page /products would be a general listing displaying all available products, while /products/B00032G1S0 would display the detail page for that product. With Template Routes we can cover both cases with one route. By default Template Route segments are optional, meaning in the case of our products page both of these URLs will match:



Template Routes is an advanced feature that allows you unlimited flexibility in designing your URLs. Template Routes along with the other new features in ExpressionEngine 2.8 gives you more flexibility and more control than ever before. We can’t wait to see how you put it to use.

.(JavaScript must be enabled to view this email address) or share your feedback on this entry with @eecms on Twitter.

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases