Template foundations in Tridion

One of the best things about Tridion is it’s flexibility. You’re free to create your website in whatever way you see fit – which is how a good Content Management System should be.

The downside of all this flexibility is that the prospect of a completely blank Tridion Page Template can be a little daunting.

Well, I’m here to tell you that having used Tridion for almost a decade you could do much worse than follow this approach.

Note: this assumes you are using the standard templating model, not the newer Compound Template system. The Compound Templating model allows for tighter integration with Visual Studio among other things.

The Page Template

It’s pretty common to base a Tridion Page Template around a for…each loop that runs through the Component Presentations collection and deals with each Component in turn.

Dim oCP
For Each oCP in Page.ComponentPresentations
' DO STUFF HERE
Next

Our technique also loops through the contents of the Page and examines each Component as it goes but we specifically look at the name of each Component Template. This is because we have previously added a little something to the name of each Component Template to help us out.

In the Tridion CMS, content authors choose from a list of templates whenever they add an item to a webpage. This list contains the name of the template – it doesn’t give a preview or explain what the template will actually do – all the author has to go by is the name.

As well as that, there is only one list of Component Presentations per Page. This means that on, for example, a three-column page layout there is no way for an author to specify which column their item is meant to appear in.

However, by adding a token of some kind to the template name, we can kill two birds with one stone – tell our authors what each template is going to do and give our Page Template code a useful hook to work with.

Article (L)
Article (M)
Article (R)

The list above represents three Component Templates. All three do the same thing but the letter in brackets indicates where on the page the item will end up (left, middle or right column).

In our Page Template we maintain three variables: strLeftColumn, strMiddleColumn and strRightColumn.

During our for…each loop we look for the letter in brackets and if it’s one we recognise then we save the output to the relevant variable.

When the loop is complete the three variables will contain the contents of each column. Now it’s simply a matter of writing out the variables in the appropriate parts of the Page Template.

<html>
<body>
<div id="left">[%=strLeftColumn%]</div>
<div id="middle">[%=strMiddleColumn%]</div>
<div id="right">[%=strRightColumn%]</div>
</body>
</html>

Staying DRY

The problem with this approach is that you end up with a rapidly expanding list of Component Templates that contain the same code. If you want to add content to a header section or you define a new area below the three main columns, you need two further templates called:

Article (H)
Article (F)

and before long things start to get out of hand.

Fortunately there is a clever trick we can play on Tridion to avoid what would otherwise be a maintainability nightmare.

Pick one of the Component Templates to be the original and replace the code in the others with nothing but the following line:

WriteOut RenderedContent(Component.Render({uri}))

where {uri} is the URI of your original Component Template. This line pulls the code from one template into another, meaning you can create as many clones as you need whilst maintaining only one real template.

Used in combination with the other techniques described above, you have an elegant way to start building your Tridion-based website.