About Sitellite       Screenshots       Downloads       Forge      Documentation       Community       Support

You are here: Home / Documentation / How To: XT Template Essentials

How To: XT Template Essentials

Loops

It is easy to think of examples of situations where you need to loop through data to present it: Any list of things will do.  Therefore, the ability to loop through data while rendering it into a template is a valuable one.  XT, like most template languages, has such a facility, and a rather powerful one at that.  Let's look at a very basic example to start with:

<xt:tpl>
    <table border="2">
        <tr xt:loop="item php: range (1, 5)">
            <td xt:content="loop/item/number">1</td>
        </tr>
    </table>
</xt:tpl>

The loop shown above will render five table cells in a row with the numbers 1 through 5 in them.  As you can see, you refer to the loop values through the "loop/item" object, which corresponds to the name of the variable in the loop expression.  Consider the following PHP code for an understanding of the use of "loop/item":

<?php

foreach (range (1, 5) as $item) {
    echo $item;
}

?>

"loop/item" corresponds to "$item" in the above code.  However, the addition of the "/number" should appear peculiar as well.  The reason for this is that a loop object in XT contains more than simply the value of the current loop item.  These additional attributes are referred to by name, as is any other property of an object, and they are:

AttributeDescription
index
The actual index key of the loop item.  In cases where you would be looping through named key/value pairs, index would contain the name.
number
The numeric index of the loop item, starting at 1 and increasing by 1 each time.
length
The number of items in the loop.
start
Whether the current item is the first or not.
end
Whether the current item is the last or not.
value
The loop value in cases where the items being looped through are not objects or arrays.  This would be the opposite of index in such cases.
even
Whether the index value (not the number) is even or not.
odd
Whether the index value (not the number) is odd or not.

Now let's look at a more complex example:
<xt:tpl>
<table border="2">
<xt:loop through="lang intl/languages">
<tr>
<td xt:content="loop/lang/index">en</td>
<td xt:content="loop/lang/name">English</td>
</tr>
</xt:loop>
</table>
</xt:tpl>
Here you can see where the power of XT's looping constructs can come into play, allowing for the ability to easily loop through and utilize complex data structures.

Next Up: Includes

Page 1: Introduction
Page 2: First Steps
Page 3: Substitutions (Part 2)
Page 4: Conditions
Page 5: Loops
Page 6: Includes
Page 7: Boxes
Page 8: I18n
Page 9: XT Object Reference

All Tutorials

Members

Note: You can use your SitelliteForge.com account here and vice versa.

Username

Password

Forgot your password?

Not a member? Click here to register

Sitellite 5 Beta


Copyright © 2008, SIMIAN systems Inc.
All rights reserved. Privacy policy
Some of the icons on this site were created by the Gnome Project.