About Sitellite       Screenshots       Downloads       Forge      Documentation       Community       Support

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

How To: XT Template Essentials

Conditions

To illustrate how conditions work, here's a brief example which I'll explain after:

<xt:tpl>
    <h1
        xt:content="title"
        xt:condition="php: not empty (object.title)"
    >Page title</h1>
</xt:tpl>

Essentially, a condition is a statement that evaluates to be true or false.  In this case, we want to display the <h1></h1> tags only if the title value is not empty.  If the condition returns false, the entire tag will not be shown.

Conditions are handy for controlling what elements to show, depending on whatever logical factors you wish to use.  Let's look at a more complex example, using condition tags instead of just the attribute.

<xt:tpl>
    <xt:condition>

        <xt:if expr="php: session_admin ()">
            <p>
Logged in as admin user <xt:var name="session/username" />.
</p>
        </xt:if>

        <xt:elseif expr="php: session_valid ()">
            <p>
Logged in as <xt:var name="session/username" />.
</p>
        </xt:elseif>

        <xt:else>
            <p>
Not logged in. <a
                 href="${site/prefix}/index/sitellite-user-login-action"
             >Click here to log in</a>.
</p>
        </xt:else>

    </xt:condition>
</xt:tpl>

This example checks if the user is logged in, and displays a different message if they are or aren't.  Let's break it down into pieces to see how XT understands it.

First, there are <xt:condition></xt:condition> wrapper tags around the entire condition.  Full XT conditions require these tags to group a set of <xt:if> <xt:elseif> and <xt:else> tags together to provide multi-condition capabilities.  This makes XT very nearly a complete programming language in its own right, together with loops and variables.

Next, we have the <xt:if></xt:if> tag with an expr="" attribute.  This attribute calls the Sitellite function session_admin(), which tells you whether the user is logged in as an administrator (see the SAF Session documentation for more info about that).  If this evaluates to true, then everything between those tags will be shown.  If not, they disappear, and XT moves on to look for an <xt:elseif> or <xt:else> condition.

The <xt:elseif></xt:elseif> tag comes next, using the Sitellite function session_valid() to determine whether the user is logged in at all or whether they are browsing anonymously.  If they are logged in, everything between those tags is shown and the condition exits.  When a condition evaluates to true, the entire condition exits, skipping any subsequent <xt:elseif> or <xt:else> tags below.

The <xt:else> condition would be shown only when none of the above conditions are met.  This is essentially a fallback action (ie. "if all else fails, do this").  As with <xt:elseif>, <xt:else> is optional, in which case often the xt:condition attribute would be a better solution.  What the <xt:else> does here is display a link for the anonymous user to log in if they'd like.

Previewing this outside of Sitellite (ie. directly through the browser) will show you the output of all of the conditions.  In this way, you can preview the results of each without having to test each condition, visually.  Of course, functionally you always want to test each possible condition to ensure that it works as you'd expect.

Next Up: Loops

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.