TVs & Chunks = BFF <3

I’ve been building all of my websites, both professional and personal, on MODx for over a year now (probably more than two years, if you count the months that I was using Etomite, which is where MODx has its roots). I’ve said it before: MODx is the best CMS I’ve worked with, ever, by a long shot. I’ve found no limits when it comes to expandability; most things are easy to build, and pretty much everything you can imagine is doable, because building additional functionality using PHP is easy as pie.

Enough said; bragging about MODx is not the purpose of this article. This article is intended for anyone who has done any website development on top of MODx (which is also a framework). You don’t have to think of yourself as a professional developer or anything like that. You just need to understand the terminology of MODx components, because I’m not going to explain them. Refer to the MODx documentation for that.

This article is about chunks and template variables (TVs), and how they love each other very, very much.

There are many different content types for TVs. There are text content types, which are just text fields and textareas. There are also content types like list boxes and check box arrays, which have multiple choices that need to be preset. When you set up the choices for these types of TVs, the syntax is something like this:

Display Name 1==Value 1||Display Name 2==Value 2||Display Name 3==Value 3

The value of a choice is very flexible; it can be a string or a number, or you can really take advantage of MODx’s facilities and use a chunk or even a snippet. Remember that a snippet is a bit of PHP script that you can pass variables to, which means that you can create a lot of intelligence in your system by using them. A chunk, on the other hand, is just a bit of HTML; you can’t pass variables to it, and it doesn’t process anything. A chunk is a chunk, and that’s it.

The Problem with TVs

In my time of developing with MODx, I’ve been able to experience some of the long term effects of certain choices that I’ve made. In the past, when I would set up multiple choice TVs, I would set numerous snippets as their values. The snippets would usually do something simple, like load up previews of the latest 5 articles, or format and display the entire article of the current document, or create a menu. Then on each page that used the templates that the particular TV was connected to, I could use those choices to tell my system what I wanted to display on that page. I started running into problems a few months later, however, when I wanted to change the values of those choices, so that, for example, the latest 5 articles might be formatted in a slightly different way. I expected that, if I changed the snippet in that choice for that TV, that every page that had that choice would consequently change the way that it formated the latest 5 articles. What I discovered quickly, however, was that MODx caches the values of the choices that you make for each TV under each document, so if I change the value of a choice in a TV, all of the documents will still have the old value cached. The new value will only be reflected on new documents; the old value will remain in the old documents unless I go back to each document, one-by-one, and deselect that choice, save the document, then reselect the choice and save again. Then the cached value will be reset.

Here’s a quick example. Say I have a TV called “SideBarContent“. The TV is a list box, and the choices are as follows:

Article Menu==[[Wayfinder? &outerTpl=`menuOuter` &innerTpl=`menuInner` &rowTpl=`menuRow` &startId=`6`]]||Comic Previews=[[Ditto? &tpl=`ComicPreviewTemplate` &display=`3` &startID=`45`]]

This TV has two choices, and the values of each choice is a snippet call. It will allow me to choose, on a per-page basis, what I want to show on that page, either the article menu or a list of comic strip previews. Good enough. But then a few months later, maybe I decide that I want to switch the template for the comic previews and change the number of previews that are displayed. So I go back into the TV settings, and change it like this:

Article Menu==[[Wayfinder? &outerTpl=`menuOuter` &innerTpl=`menuInner` &rowTpl=`menuRow` &startId=`6`]]||Comic Previews=[[Ditto? &tpl=`NewComicPreviewTemplate` &display=`5` &startID=`45`]]

I do this expecting that all of the documents that have the comic preview choice selected, will use the new template and show 5 previews instead of 3. What I will find instead, is that only documents that are created after this change will reflect the new values; all of the old documents will remain unchanged. This is because when the choice was selected for this TV in the old documents, the system cached the old snippet call with all the old parameters. This makes the system run a bit faster, but can cause problems when changes like this need to be made.

So what makes chunks so special?

So this is where chunks can become very handy. I said before that chunks are just bits of HTML markup; that’s true, but there’s more to it. I also said that they can’t process anything; that’s true too, at least not in and of themselves. One thing about chunks that makes them extremely useful is that you can embed snippets in them. When you do this, you give them all the power of that snippet. And in the case of setting TV choice values, you prevent the system from caching the snippet call.

So now I can set the TV list box up like this:

Article Menu=={{ArticleMenu}}||Comic Previews=={{ComicPreviews}}

In the ComicPreviews chunk, I can put the following snippet:

[[Ditto? &tpl=`ComicPreviewTemplate` &display=`3` &startID=`45`]]

Now when I select this choice in my documents, the system will cache the call to my chunk, which in turn calls whatever snippets are in that chunk. If I want to change the parameters of my snippet later down the road, I just go to my chunk and change it’s contents to whatever I want. This will produce the cascading effect we’re looking for; the changes will be reflected in all documents that have this choice selected, new and old.


No comments for “TVs & Chunks = BFF <3”

Post a comment