How to setup a loop from within a Movable Type plugin

Note: refer to the existing tutorials on the Movable Type website for information setting up the basics of a plugin.

This is a simple example of how to use the <mt:loop> template tag within a plug-in:


my $iter = MT::Entry->load_iter({class => 'page'});
my @entry;

while (my $entry = $iter->())
{    #@entry holds a series of hashes for the loop
    #You'd set <mt:loop name="entries"> based on the params below
    #then call <$mt:var name="title"$> inside the loop to access title
    push (@entry, {title => $entry->title});
}

%params[entries] = \@entry;    #Set it to a reference of @entry


So, in a nutshell, the code is supposed to load an iterator for the MT::Entry object, and loop through it, adding a hash of values that will be used in the loop. The list that holds the hashes is then added to the %params hash that is going to be used by the loop. When you set up the loop, by telling it to use the value "entries" from the %params hash, it will go through each item, which is a hash, and you can access each of the key/value pairs using <$mt:var$> as I have shown below.


Now, here's the example from the template file (.tmpl):


<mt:loop name="entries">
    <select>
        <option><$mt:var name="title"$></option>
    </select>
</mt:loop>

That code will generate a combo box that has all of your Movable Type pages' titles listed in it.

Leave a comment

March 2010

Sun Mon Tue Wed Thu Fri Sat
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Recent Entries

The three purposes of the federal income tax law
Businesses will spend about 3.4 billion man-hours and individuals about 1.7 billion hours figuring out their taxes this year.…
Progress of a different sort
You know we have reached a level of decadence seldom seen in the history of the West when our women…
And police wonder why the public rarely trusts them
But there is some good news to report here, too. The Maryland state law, as noted, is the first…

Subscribe

Advertisements

OpenID accepted here Learn more about OpenID