Not a developer? Go to



Loop over the values in a hash or an array (see mt:If).

<mt:Loop name="author_ids">
    <$mt:Var name="__key__"$> = <$mt:Var name="__value__"$>

This tag is primarily used for processing a Perl array of hashref data in the application templates, but can also be used in blog templates.

This tag’s heritage comes from the CPAN HTML::Template module and it’s TMPL_LOOP tag and offers similar capabilities.

This tag can also handle a hashref variable, which causes it to loop over all keys in the hash.



The name of the array, hash data to process.


Alias of name.


Causes the data in the given array to be resorted in the manner specified.

By default the values are ouput in the order they are stored in the array/hash.

Sort by keys in reverse order:

<mt:Loop name="author_ids" sort_by="key reverse">
    <!-- something here -->

Multiple space-separated values may be used:

  • reverse - reverses the order
  • key - sort by key
  • value - sort by value
  • numeric - sort numerically by value (only necessary when combined with value attribute)


If specified, this string will be placed in between each “row” of data produced by the loop tag.

Loop Variables

Within the tag, the following variables are assigned and may be used:


Assigned when the loop is in the first iteration.


Assigned when the loop is in the last iteration.


Assigned 1 when the loop is on odd numbered rows, 0 when even.


Assigned 1 when the loop is on even numbered rows, 0 when odd.


When looping over a hashref template variable, this variable is assigned the key currently in context.


This variable holds the value of the array or hashref element currently in context.


Add values to an array, then output:

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

My BLT sandwich contains the following ingredients: 
<mt:Loop name="sandwich" glue=", ">
    <$mt:Var name="__value__"$>

Place variables into a hash using SetHashVar and then access directly via keys using the name attribute:

<mt:SetHashVar name="my_hash">
    <$mt:Var name="foo" value="bar"$>
    <$mt:Var name="fizzle" value="fozzle"$>
<mt:Loop name="my_hash">
    <$mt:Var name="fizzle" $>
    <$mt:Var name="foo" $>

List entry categories if their label is within the set Explore, Savor, Inside, Offbeat, or Celebrate and then glue them with “, “. Using mt:SevVarBlock with function=”push” attribute and then output with mt:Loop:

    <mt:If tag="CategoryLabel" like="(Explore|Savor|Inside|Offbeat|Celebrate)">
        <mt:SetVarBlock name="featured_categories" function="push">
            <a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a>
<mt:Loop name="featured_categories">
    <$mt:Var name="__value__" trim="1"$><mt:Unless name="__last__">, </mt:Unless>

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. View a simplified version of this using Hash Variables

<$mt:Var name="author_limit" value="4"$>
<$mt:Var name="author_count" value="0"$>
<mt:Entries lastn="20">
<h2>Recent Bloggers</h2>
    <mt:If name="author_count" lt="$author_limit">
        <$mt:EntryAuthorID setvar="current_author_id"$>
        <$mt:Var name="skip" value="0"$>
        <mt:Loop name="author_ids">
            <mt:If name="current_author_id" eq="$__value__">
                <$mt:Var name="skip" value="1"$>
        <mt:Unless name="skip">
        <$mt:EntryAuthorDisplayName encode_html="1"$><br />
        <a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a>
        <$mt:EntryAuthorID setvar="author_id"$>
        <$mt:Var name="author_ids" function="push" value="$author_id"$>
    <$mt:SetVar name="author_count" op="++"$>

Leave a Comment