Not a developer? Go to MovableType.com

Documentation

Loop

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__"$>
</mt:Loop>

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.

Attributes

name

The name of the array, hash data to process.

var

Alias of name.

sort_by

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 -->
</mt:Loop>

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)

glue

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:

first

Assigned when the loop is in the first iteration.

last

Assigned when the loop is in the last iteration.

odd

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

even

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

key

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

value

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

Examples

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__"$>
</mt:Loop>

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:SetHashVar>
<mt:Loop name="my_hash">
    <$mt:Var name="fizzle" $>
    <$mt:Var name="foo" $>
</mt:Loop>

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:EntryCategories>
    <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:SetVarBlock>
    </mt:If>
</mt:EntryCategories>
<mt:Loop name="featured_categories">
    <$mt:Var name="__value__" trim="1"$><mt:Unless name="__last__">, </mt:Unless>
</mt:Loop>

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">
    <mt:EntriesHeader>
<h2>Recent Bloggers</h2>
<ul>
    </mt:EntriesHeader>
    <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:If>
        </mt:Loop>
        <mt:Unless name="skip">
    <li>
        <$mt:EntryAuthorDisplayName encode_html="1"$><br />
        <a href="<$mt:EntryPermalink$>"><$mt:EntryTitle$></a>
    </li>
        </mt:Unless>
        <$mt:EntryAuthorID setvar="author_id"$>
        <$mt:Var name="author_ids" function="push" value="$author_id"$>
    </mt:If>
    <$mt:SetVar name="author_count" op="++"$>
    <mt:EntriesFooter>
</ul>
    </mt:EntriesFooter>
</mt:Entries>
Back

Leave a Comment