Not a developer? Go to


Variable Interpolation

Movable Type allows for designers and developers to embed variables inside of other template tags. This gives them the flexibility of having a single tag produce a variety of outputs depending upon its context or what variables are passed into it. For example, say you wanted to display a list of related entries based upon what entry the reader happen to be looking at. You wanted to do this by finding the most recent entries from categories shared by the current entry. Make sense?

Let’s compare two examples. The first displays a list of entries tagged “news” or “current events.”

<mt:entries tags="News OR Current Events">
<li><a href="<mt:entrypermalink>"><mt:entrytitle></a></li>

Now, let’s tweak that template code to display a list of entries using a variable to select which tags to filter the list by:

<mt:setvarblock name="tags"><mt:entrytags glue=" OR "/></mt:setvarblock>
<mt:entries tags="$tags">
<li><a href="<mt:entrypermalink>"><mt:entrytitle></a></li>

In this example, the variable is denoted using the dollar sign ($) followed by the variable name. Note that the dollar sign is not needed when declaring the variable using the <mt:SetVarBlock> template tag.

Starting with Movable Type 4.0, all template tag attributes can process variables in this fashion, opening the door for far advanced and adaptive templates.



Jay Allen

Jay Allen on May 23, 2008, 7:21 p.m. Reply

Another way to use variable interpolation is in the name attribute

# Set the output variable
<mt:var name="blah" value="Yay!">

# Assigns 'blah' to $meta
<mt:var name="meta" value="blah">    

# Double interpolation!
<mt:var name="$meta">  # Outputs: Yay!

How is that useful? Well imagine a situation where you have a global template module with just the following:

<mt:ignore> Show all blogs except 3, 6 and 9 </mt:ignore>
<mt:blogs exclude_blogs="3,6,9">
    <mt:setvarblock name="meta">show_blog_<$mt:blogid /></mt:setvarblock>
    <mt:var name="$meta" value="1">

Then in your blog template to display something based on whether a blog is public:

<mt:setvarblock name="is_public_blog">show_blog_<$mt:blogid /></mt:setvarblock>
<mt:if name="$is_public_blog">
    # Display blog data
François Nonnenmacher

François Nonnenmacher on July 11, 2010, 8:22 p.m. Reply

The following syntax on the second example will not work:

<mt:entrytags glue=" OR "/>

It’s a block tag, so it needs to be written as follows:

<mt:entrytags glue=" OR "><$mt:tagname$></mt:entrytags>