Not a developer? Go to MovableType.com

Documentation

Var

A function tag used to store (like the <mt:SetVar>) and return values from variables.

This tag is one of the most useful tags when digging deeper into creating templates with Movable Type.

Basic Example

  1. Set a variable (no output when setting the value of a variable):

    <$mt:Var name="foo" value="bar"$>
    
  2. later in the code, output the value of the variable:

    <$mt:Var name="foo"$>
    

    Output:

    bar
    

Tip: Use the <mt:SetVarBlock> tag to store the output of a set of template tags in a variable:

<mt:SetVarBlock name="entry_ids">
  <mt:Entries glue=","><$mt:EntryID$></mt:Entries>
</mt:SetVarBlock>

Variables as attribute values

<$mt:Var name="latest_entries" value="10"$>
The most recent <$mt:Var name="latest_entries"$> entries:
<ul>
<mt:Entries lastn="$latest_entries">
    <li><a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a></li>
</mt:Entries>
<ul>

Tip: Use the setvar modifier to set the value of a variable rather than output the value:

<$mt:BlogID setvar="foo"$>

Attributes

Note: Support for various attributes depends on the version of Movable Type.

The most common attributes are in bold.

append

Used to append a value to the end of an existing variable. Opposite of prepend.

Values 1 and 0 (default).

<$mt:Var name="foo" value="bar"$>
<$mt:Var name="foo" value="baz" append="1"$>
<$mt:Var name="foo"$>

Output:

barbaz

default

A value returned when the variable’s value is a empty string or not defined.

<$mt:Var name="foo" default="bar"$>

A value of “0” does not trigger the default attribute. Use a conditional such as <mt:If> or <mt:Unless> tag instead:

<$mt:Var name="foo" value="0"$>
<mt:If name="foo">
    foo is a non-zero value.
<mt:Else>
    foo is zero or another empty string.
</mt:If>

function

The function attribute supports diffent values for array and hash variables.

For array variables:

  • count - Returns the number of elements in the array template variable.
  • pop - Takes an element from the end of the array (last element).
  • push - Adds an element onto the end of the array.
  • shift - Takes an element from the front of the array (index 0). Only works with mt:GetVar.
  • unshift - Adds the element to the front of the array (index 0).
  • glue - value used to join the values of an array together.
  • index - Identifies an element of an array template variable.

For hash variables:

  • count - Returns the number of keys present in the hash template variable.
  • default - If the variable is undefined or empty, this value will be output instead.
  • delete - Only valid when used with the ‘key’ attribute, or if a key is present in the variable name.
  • key - Identifies a key of a hash template variable.
  • to_json - Formats the variable in JSON notation.

See examples for array and hash variables below.

name

Required attribute identifying the template variable to be assigned or displayed.

The name attribute value should be a string made up of only alphanumeric characters and underscores (“_”) in order to not conflict with variable interpolation. Curly brackets (“{ }” may be used for specifying hash values see <mt:SetHashVar>).

It’s best practice to keep variable names as simple, short and memorable as possible. (Note: prior to Movable Type 4, variable names were less constrained, but invalid characters such as spaces and symbols in particular are unsupported.)

Template variables may also be arrays or hash variables, in which case you may want to reference a specific element instead of the array or hash itself.

This selects the second element from the ‘foo’ array variable:

<$mt:Var name="foo[1]"$>

This selects the ‘bar’ element from the ‘foo’ hash variable:

<$mt:Var name="foo{bar}"$>

Sometimes you want to obtain the value of a function that is applied to a variable (see the function attribute). This will obtain the number of elements in the ‘foo’ array variable:

<$mt:Var name="count(foo)"$>

op

Allows the application of a number of mathematical operators to the value of the variable.

Note: when using the op attribute, the value attribute is used to define the second operand instead of to set the value of the variable. If the calculated value of the operation needs to be stored, use the setvar attribute.

Examples:

<$mt:Var name="days" value="10"$> (set the value of the variable using "value". The [`<mt:SetVar>`](/tags/setvar) tag could also be used here.)
<$mt:Var name="days"$> ($days = 10)
<$mt:Var name="days" op="/" value="2"$> ($days divide by 2 = 5)
<$mt:Var name="days" op="*" value="3" setvar="days_multiplied_by_three"$> ($days multiplied by 3 = 30 stored in a new variable using "setvar")
<$mt:Var name="days_multiplied_by_three"$> ($days_multiplied_by_three = 30)
<$mt:Var name="days" op="+" value="1" setvar="days"$> ($days plus 1 = 11 used to update the value of the "days" variable using "setvar")
<$mt:Var name="days" op="%" value="3"$> (remainder of $days divided by 3 = 2)
<$mt:Var name="days" op="++"$> ($days plus 1 = 12)

Output

 (set the var)
10 (output = 10)
5 (output = 5)
 (output of 30 used to set the valuable of a new variable)
30 (output = 30)
 (output of 11 used to update the value of the "days" variable)
2 (output = 2)
12 (output = 12)

See the <mt:If> tag for supported values for the op attribute and operations examples on the wiki.

prepend

Used to add a value to the beginning of an existing variable. Opposite of append.

Values 1 and 0 (default).

<$mt:Var name="foo" value="bar"$>
<$mt:Var name="foo" value="baz" prepend="1"$>
<$mt:Var name="foo"$>

Output:

bazbar

value

Causes the variable to be assigned the value specified. In this way, this tag is useful for setting variables instead of using <$mt:SetVar$>, <mt:SetVars>, or <mt:SetVarBlock>.

The following are equivalent, they all set the the value of the variable “foo” to “bar”:

<$mt:Var name="foo" value="bar"$>
<$mt:SetVar name="foo" value="bar"$>
<mt:SetVarBlock name="foo">bar</mt:SetVarBlock>

The value can contain anything other than a double quote. If the value is long or contains a double quote, use <mt:SetVarBlock> instead.

If provided with the op attribute, value provides the operand for the specified mathematical operation.

Six Apart has four offices, added four different ways as values of the “offices” array. Then output using the <mt:Loop> tag:

<$mt:SetVar name="offices[0]" value="San Francisco"$>
<$mt:SetVar name="unshift(offices)" value="Tokyo"$>
<mt:SetVarBlock name="offices[2]">Paris</mt:SetVarBlock>
<$mt:SetVar name="offices" index="3" value="New York"$>
<mt:Loop name="offices" glue=", "><$mt:Var name="__value__"$></mt:Loop>

var

Alias of name.

Examples

Variables with Operator

This example outputs the count of entries in the loop which have the tag specified; in this case the tag is set to “Foo”. The variable “tag_count” is initialized and then in the loop, if the entry is tagged with the value of “tag_label”, then the “tag_count” variable is incremented. Finally the “tag_count” variable is output if it is a non-zero or non-empty value.

<$mt:Var name="tag_label" value="Foo"$>
<$mt:Var name="tag_count" value="0"$>
<ul>
<mt:Entries>
    <li>
        <$mt:EntryTitle$>
    <mt:EntryIfTagged tag="$tag_label">
        is tagged "<$mt:Var name="tag_label"$>"
        <$mt:Var name="tag_count" op="++" setvar="tag_count"$>
    </mt:EntryIfTagged>
    <mt:If tag="EntryTags">
        (tags: <mt:EntryTags glue=", "><$mt:TagLabel$></mt:EntryTags>)
    </mt:If>
    </li>
</mt:Entries>
</ul>
<mt:If name="tag_count">
    <p>On this page there are <$mt:Var name="tag_count"$> entries tagged "<$mt:Var name="tag_label"$>".</p>
</mt:If>

Array Variables

Add items to an array “sandwich”:

<$mt:Var name="sandwich" index="0" value="bagel"$>
<$mt:Var name="sandwich" index="1" value="cream cheese"$>
<$mt:Var name="sandwich" index="2" value="lox"$>

An alternate syntax, just for reference:

<$mt:Var name="sandwich[1]" value="bagel"$>
<$mt:Var name="sandwich[2]" value="cream cheese"$>
<$mt:Var name="sandwich[3]" value="lox"$>

Let’s take an item off the end of the array and then add a new to the end of the array:

<$mt:Var name="sandwich" function="pop" setvar="removed_ingredient"$>
<$mt:Var name="sandwich" function="push" value="tomato"$>

Output them glued with comma and space. Then is concatenated with a period using the cat modifier.

The sandwich has <$mt:Var name="count(sandwich)"$> ingredients: <$mt:Var glue=", " cat="." name="sandwich"$> The removed item is: <$mt:Var name="removed_ingredient"$>.

Final output:

The sandwich has 3 ingredients: bagel, cream cheese, tomato. The removed item is: lox.

The ingredients can also be output using the <mt:Loop> tag with template loop meta variables:

<mt:Loop name="sandwich">
    <li><$mt:Var name="__value__"$></li>
</mt:Loop>

Hash Variables

Loop through the last 20 entries and list each author once (skip if already found), limit to 4. This code assumes that there will be at least 4 unique bloggers in the last 20 entries. If not increase the lastn value.

<$mt:Var name="author_limit" value="4"$>
<$mt:Var name="author_count" value="0"$>
<mt:Entries lastn="20">
    <mt:EntriesHeader>
<h2>Recent Bloggers</h2>
<ul>
    </mt:EntriesHeader>
    <mt:If name="author_count" lt="$author_limit">
        <$mt:EntryAuthorID setvar="current_author_id"$>
        <mt:Unless name="recent_bloggers{$current_author_id}">
            <li>
                <$mt:EntryAuthorDisplayName encode_html="1"$><br />
                <a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a>
            </li>
            <$mt:SetVar name="author_count" op="++"$>
            <$mt:SetVar name="recent_bloggers{$current_author_id}" value="1"$>
        </mt:Unless>
    </mt:If>
    <mt:EntriesFooter>
</ul>
    </mt:EntriesFooter>
</mt:Entries>

Loop though the blogs in the install and ouput an additional message for blog with the id of 1:

<mt:SetVarBlock name="BlogExtras{1}">
    This the blog with the ID of 1
</mt:SetVarBlock>
<mt:Blogs>
    <$mt:BlogID setvar="BlogID"$>
    <div>
        <a href="<$mt:BlogURL$>"><$mt:BlogName$></a>
        <mt:If name="BlogExtras{$BlogID}">
            <$mt:Var name="BlogExtras{$BlogID}"$>
        </mt:If>
    </div>
</mt:Blogs>
Back

6 Comments

Su

Su on June 27, 2008, 5:17 p.m. Reply

The op attribute is defined in the wiki .

JonathanS

JonathanS on September 1, 2008, 1:12 a.m. Reply

For those of us coming to template variables for the first time, and redirected here from the deprecated SetVar, an example of using Var to set a variable would be useful. Thus:

<mt:Var name="foo" value="bar">
JonathanS

JonathanS on September 1, 2008, 6:14 a.m. Reply

In some circumstances, <mt:Var ... > isn’t evaluated. An example would be:

<mt:Entries field:foo="<mt:Var name="bar">">

…doesn’t work. The alternative syntax $name, however, does work:

<mt:Entries field:foo="$name">

More about this at in the forumshere.

Beau Smith

Beau Smith on April 28, 2009, 12:09 a.m. Reply

Example

The following code will grab the ids of the last 5 entry tagged @foo then the next 5 entry ids tagged @foo or @bar, whichever comes first. Once these entry ids are pushed onto the array, the values are then pulled off the array using the for loop to output the custom list of entries:

<mt:Entries include_blogs="1" tags="@foo" lastn="5">
    <mt:SetVarBlock name="entry_ids" function="push"><$mt:EntryID$></mt:SetVarBlock>
</mt:Entries>
<mt:Entries include_blogs="1" tags="@foo OR @bar" lastn="5" unique="1">
    <mt:SetVarBlock name="entry_ids" function="push"><$mt:EntryID$></mt:SetVarBlock>
</mt:Entries>
<mt:Loop name="entry_ids">
    <mt:Entries id="$__value__" include_blogs="1">
        <$mt:EntryTitle$>
    </mt:Entries>
</mt:Loop>
Beau Smith

Beau Smith on October 30, 2009, 1:19 a.m. Reply

Here’s a pretty advanced method of adding breadcrumbs on a site with many blogs using array variables

Jay Allen

Jay Allen on October 30, 2009, 1:57 a.m. Reply

Under Array Variables, you have the following:

    <mt:Var name="sandwich" function="push" value="tomato">

However, there’s another form for “push” (and all other “function” attribute values) which I tend to prefer because it’s a whole lot shorter:

    <mt:Var name="push(sandwich)" value="tomato">

Unfortunately, that’s pretty much the only place/way you can use that as all of the following fail (or did last time I checked):

  • Using a function in the “value” attribute:

    <mt:Var name="push(sandwich)" value="pop(ingredient)">
    
  • Using an actual variable inside of the function parentheses for double interpolation:

    <mt:Var name="thing" value="sandwich">   
    <mt:Var name="push($thing)" value="tomato">
    
  • Surprisingly, I seem to remember that you also couldn’t use a function in SetVarBlock:

    <mt:SetVarBlock name="push(sandwich)">   
        tomato   
        Tomato!   
        TOMATO!!!!   
    </mt:SetVarBlock>
    

Anyway, on a different issue, under Hash variables, are you sure the following works?

    <mt:Unless name="recent_bloggers{$current_author_id}">

Perhaps this has been added in 4.3x, but in 4.2x and earlier, I believe that the only variable interpolation that was done was when either:

  1. The name or value contained nothing but the variable: <mt:var name="$myvar">

  2. The variable was contained within a function in the name attribute (as shown at the beginning of this comment)

If so, then hooray! However, you’re going to have a lot of frustrated users still on 4.2 who are trying and failing to make some examples contained here in the docs work on their installs.

Leave a Comment