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.

July 2009

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  

Tips

OpenID accepted here Learn more about OpenID

Creative Commons License
This blog is licensed under a Creative Commons License.
Powered by Movable Type 4.261