In this post, we explore reusability of Dreamweaver and Razor Templates, as well as how to break up a single DWT or Razor design into multiple design building blocks, within the same template.
â€œ IfÂ you are updating the same code in multiple places,
you are doing it wrong.Â â€
In grade school weÂ learned how to factor algebraic expressions, like so:
ax + bx = (a + b)x
This is nice because, among other things, it allows us to write the expression more concisely, with only one x. Say we had to go back through our work and replace all x‘s with y‘s. In the latter expression, we would only have to do that in one place, as opposed to two, saving our eraser.
When I think back toÂ this, it draws immediate analogies to code reuse, or code “factoring” as I like to call it. In theÂ small algebra example above, the advantages are less obvious. But imagine you are writing several functions in C#, each using some identical snippet of code. Instead of copying and pasting that snippet of code into each function, we put it in another function, and call that function whenever the snippet is needed. The advantages are huge – mainly that changes only need to be made in one place (so we are less likely to forget to make changes or to make inconsistent changes). We save time by only needing to update in one place, and we end up with much fewer lines of code, ideally. This is a standard practice that all good programmers use. Basically, if you are updating the same code in multiple places, you are doing it wrong. Think of it this way: you should always cut and paste code, never copy and paste.
But what about Tridion templating? In particular, template designs such as Dreamweaver Templates (DWTs)Â or Razor Templates. Generally, the pattern is to have several template building blocks that run before and after a single design:
What if several designs share the same first 10 or so lines (e.g. XMLÂ namespace references)? Is there a way to avoid copying and pasting these lines? Can we somehow break the design down into pieces, and reuse some of these pieces in several different templates? There is oneÂ solutionÂ that was shown to me by my boss. This solution uses parameter schemas along with a custom syntax for field replacement, to create reusable markup tokens that can be pulled into aÂ DWT. That is an interestingÂ solution. But I wanted something simpler and more direct. I wanted a way to have several DWT building blocks that I could combine in any order. For example:
Now I canÂ go ahead andÂ reuse Test_Design_1 or Test_Design_2 in any other component or page template that requires that precise snippet of design. Of course, this is a very simple example, onlyÂ meant only as a proof of concept. But it demonstrates a simple techniqueÂ for reuse of partial designÂ “snippets”. In theory, weÂ could create any level of sophisticated building blocks,Â allowing for greater and greater levels of designÂ reuse.
There are some limitations to this technique. It is certainly not as powerful as code refactoring techniques discussed above for, say, C# code. We can’t break the designsÂ into functions, and so each designÂ would likely have to be a standalone snippet of markup, having fewÂ dependencies on other design(s)Â (although in practice, this is often the case).Â There may also beÂ some overhead related to passing the package through the template, as discussed in a post by Tanner Brine. But the benefits of enabling extensive reuse of partial designs will likely outweigh these drawbacks.