January 27, 2009
Blog » The beauty of MT template modularization
When the Movable Type team released MT 4.0 it featured major change in the default template set: The templates abstracted out repeated code and put them into modules which could be reused not only for the blog but for all blogs in the system.
The main benefit of modularization is clear: When you want to make a change that affects more than one template (e.g. adding a section to the header of every page of your site), you only have to edit a single template instead of making redundant changes to potentially fifteen or twenty templates.
However, there were many detractors who decried the change because of its one main drawback: Complexity. In order to make a single snippet of template code work in all the places it needs to, conditional statements must be added to allow it to publish differentially for those use cases which deviate from the norm.
How Expert Modularization Neutralizes a Newbie Mistake
While the concerns and complaints are certainly valid ones -- especially for new users who are intimidated by the extra code -- I have an anecdote that I must share to illustrate yet another way that this change was not only positive, but essential.
We are currently working on and nearing the end of a complete redesign for a client whose site is powered by MT 3.x. At the beginning of the project we decided that we would scrap the 3.x templates and start fresh putting an emphasis -- as we always do -- on both flexibility and maintainability after the project. Since we've been modularizing the templates for years, using the MT 4 model (if not the actual template code) was a natural decision as the MT team did a number of things right in their design.
Late last night, I happened to be editing a particular template but my changes didn't seem to be taking. It was as if MT simply wasn't rebuilding the template anymore despite me making obvious and drastic changes to the template like, for example, replacing all of the code with "WTF?!?". Like any good developer who never thinks they are at fault, I spent 30 minutes debugging the problem, checking the error logs, disabling plugins, tracing execution of the application. Eventually, I found the problem: I had inadvertently switched tabs in the browser and copy/pasted my template code (which I was working on in TextMate) into the wrong template, the Main Index.
So, not only had I wasted nearly forty-five minutes on this problem but I had destroyed the home page entirely. Normally, we keep template code in Subversion so that we have versioned backups but not so for this project. So, because of my cursed lack of preparation and my fatigue-induced flub, I was faced with the unhappy task of rebuilding the entire main index from scratch.
However, I'm happy to report that the entire process took less than fifteen minutes thanks in very large part to our dedication to modularized template code!
You see, one of the less trumpeted benefits of modularization is a massive reduction in the complexity of your main templates, the ones that MT actually builds. These templates now act as simple, architectural outlines of what should exist on the page instead of complex blueprints detailing each and every line of code. In fact, the rebuilt main index is only 40 lines long with extra newlines for spacing.
Dr Strangecode or How I Learned to Stop Worrying and Love the Modules
There is no doubt that the complexity of modularized templates creates a higher barrier to learning Movable Type and its template language. The abstraction and conditionalization simply takes more brainpower and concentration in order to successfully juggle all the variability. This makes it harder to truly grasp the final output and how changes to any particular module might affect that.
However, I urge all of you Movable Type newbies out there and those of you who long for the "old way of doing it" to shed your fears and embrace the model. I promise you that, in a shorter time than you can imagine, modularized templates will not only become completely understandable but you'll be rewarded a thousand times over in future development and site maintenance.
Shorter link to this entry: http://icanhaz.com/MTTemplateModularization