<$mt:Var$>
A function tag used to store (like the <mt:SetVar> tag) and output data in a template.
Basic example
Set a variable (no output when setting the value of a variable):
<$mt:Var name="foo" value="bar"$>later in the code, output the value of the variable:
<$mt:Var name="foo"$>Output:
bar
Tip: Set the value of a variable to the output of Movable Type template tags, use the <mt:SetVarBlock> tag or for function template tags, use the setvar modifier attribute to set the output to a variable instead of being output:
<$mt:BlogID setvar="foo"$>
Attributes ¶
Note: Support for various attributes depends on the version of Movable Type.
Note: 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 ¶
For array template variables, the function attribute supports:
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).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 template variables, the function attribute supports:
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.
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:
<mt:Loop name="sandwich">
<li><$mt:Var name="__value__"$></li>
</mt:Loop>

Su
June 27, 2008 5:17 PM | Reply
The op attribute is defined in the wiki .
JonathanS
September 1, 2008 1:12 AM | 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:
JonathanS
September 1, 2008 6:14 AM | Reply
In some circumstances,
<mt:Var ... >isn't evaluated. An example would be:...doesn't work. The alternative syntax
$name, however, does work:More about this at in the forumshere.
Beau Smith
April 28, 2009 12:09 AM | 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: