Variable Interpolation
configuration
| 11 July 2010 | 
MovableType has this incredibly beautiful thing called variable interpolation. As I understand it, what this means is that you can define a variable somewhere and then reference it at another place altogether. You can even use it to supply a value for a template tag modifier, another variable, or a value for a template tag if the tag takes one.
Let’s look at an example, used on this very site, its siblings and parent. Here’s the set up, all on a single box at the same webhost. A single MT installation. There’s the parent site, gautampatel.com. Beneath that there are four existing blogs, including this one, and more are planned. The parent site has no entries of its own; it only pulls content from the subsidiary blogs. Each sub-blog has its own distinctive URL, too, a full-fledged domain name correctly mapped.
Thus:
Each blog, including the parent is individually styled. The styles of the sub-blogs do not reflect on the parent blog.
Evidently, each blog has its own URL, path and blogid. I need to identify all of these separately and then cobble all the ids together, separated by commas, so I can use Multiblog and Search effectively.
Up in the parent blog, I create a template module called System Head. It looks like this.
Here’s a line-by-line explanation:
<mt:multiblog include_blogs="all">
This sets the Multiblog block to look at all the blogs in the system.
<mt:if tag="blogname" like="(Gautam)">
This uses a logic tag, if, and, as Multiblog cycles through the blogs, the logic tag tests whether a certain condition is true using a simple regular expression.
Next, if the condition is true, use the setvar tag to set a bunch of variables: the blogid, the blogname, the blogurl. There’s an easier way of doing this in one go, using
<mt:blogid setvar="GPID">
<mt:blogname setvar="GPName">
<mt:blogurl setvar="GPURL">
Then you repeat the condition test and setting of variables for all blogs in the system.
<mt:elseif tag="blogname" like="(Bibliophage)">
<mt:blogid setvar="BibID">
<mt:blogname setvar="BibName">
<mt:elseif tag="blogname" like="(Chronicles)">
<mt:blogid setvar="COWTID">
<mt:blogname setvar="COWTName">
<mt:elseif tag="blogname" like="(Mcavity)">
<mt:blogid setvar="McavityID">
<mt:blogname setvar="McavityName">
... etc
</mt:if>
Now, for the search and other things, I want a listing of all blogs except the parent blog. So I do this.
<mt:Blogs exclude_blogs="$GPID">
<mt:setvarblock name="ThisID"><mt:blogid></mt:setvarblock>
<mt:Var name="btpush" function="push" value="$ThisID">
</mt:blogs>
Note the variable interpolation. I used $GPID to pull the blogid of the parent blog from the variable I set earlier, and I excluded that. By the way, as far as I know, <mt:blogs> is the same thing as <mt:multiblog>.
I also used <mt:setvarblock> here because I was doing this that the <setvar> tag cannot do; I was pulling a value from another tag.
Note: Be careful using <mt:setvarblock>. You should have everything between the opening and closing tags in one line, because otherwise it inserts linefeeds (naturally; it’s doing a block). Alternatively, you can use the strip_linefeeds="1" modifier, so:
<mt:setvarblock name="ThisID" strip_linefeeds="1">
What this block does is that it loops through all the blogs in the system except the parent blog, pulls their blogids, and stashes them in an array (that’s the function="push" thing you see there. Again, note the variable interpolation.
Here’s the fun part. I now call that array, using the <mt:loop> tag, get the values for each item in the array and stock them into another array.
<mt:setvarblock name="bti">
<mt:Loop name="btpush" glue=",">
<mt:Var name="__value__">
</mt:Loop>
</mt:setvarblock>
The next lines are a redundancy. Just laziness.
<mt:setvarblock name="allsearch">
<mt:Loop name="btpush" glue=",">
<mt:Var name="__value__">
</mt:Loop>
</mt:setvarblock>
<mt:setvarblock name="allblogs">
<mt:var name="bti">
</mt:setvarblock>
Now I need to use all or any of these variable anywhere in the system. So what I do is that I link the System Head template module to a file on my system. Something like mod_syshead.mtml or whatever. Then, in the each sub-blog, I recreate a copy of the System Head module (pointing it to the file’s original location; I don’t make multiple copies of these files); and then add a line to the top of the HTML Head template module — it has to be right at the top, by the way — invoking the System Head template module.
So in the Mcavity sub-blog, the HTML Head template module has this line at the very top:
<mt:include module="System Head">
That way each sub-blog operates with the same set of variables. And I can use these variables to identify which blogs should be searched, or to use a variable like GPURL to reference a location under the parent website, like so:
<mt:var name="GPURL">/images/mynicepic.jpg
Whew! Long explanation for something so seemingly trivial, but it’s not really. It’s logically true but also dense, and without MT’s marvellous variable interpolation, logic and array functions it would have been a nightmare.
Here’s the beauty and power of MT: It allows you to do so much, so elegantly and quickly with just a little patience and thought.
Tip of the hat to whoever thought of this.