Click here to register.
      

Loop De Loop

Design Squid
Other Design Articles
Loop De Loop
Meatbop · 2/13/2009 1:50 pm
Meatbop
Design Squid

This month I thought I'd take a look at one of the more common template variables available to nearly all assets in WebGUI, the Loop.  I'm sure that most folks who have templated in WebGUI know what a loop is, but there a couple of things that can be done with them that maybe you never considered.

1)Test if data is present.  Did you know that you can do a basic true false test against a loop just to see if there's anything even in it? This is extremely handy in those cases where you don't want anything to happen if there isn't any data at all in a Loop.  Here's an example with a Collaboration System  asset.


<tmpl_if attachment_loop>
    <tmpl_loop attachment_loop>
        <tmpl_if __FIRST__>
            <div class=”attachments”>
        </tmpl_if>
            <div><a href=”<tmpl_var.page.url>”><tmpl_var page.title></a></div>
        <tmpl_if __LAST__>
            </div>
        </tmpl_if>
    </tmpl_loop>
</tmpl_if>



With this method, a surrounding div can be placed around all of the attachments which can allow additional styles to be used. But even beyond that, if there are no attachments, nothing at all happens- which is exactly what we'd want if nothing was attached.  But look beyond this simple example- a Search template could be modified to display “No Results Found” instead of loading a blank page. Or perhaps if no files are loaded within a Photo Gallery a message could be displayed that it's currently empty and photos need to be added. There are plenty of options to explore, particularly because loops are so prevalent in WebGUI assets.


2) Run loops more than once. I'm not sure if it's a little known fact or if people worry that it'll break their templates, but I rarely see anyone using the same loop twice in a template. At first glance, the need for that is probably not all that obvious, but there are some great reasons why to reuse a loop. Lets take a look a Navigation asset.

<tmpl_loop page_loop>
    <tmpl_if page.isCurrent>
        <div class=”navTitle”><tmpl_var page.title></div>
    </tmpl_if>
</tmpl_loop>

<div class=”mainNav”>
    <tmpl_loop page_loop>
        <div class=”navItem”><a href=”<tmpl_var page.url>”><tmpl_var page.title></a></div>
    </tmpl_loop>
</div>


With this example, whatever the title of the page is will be displayed on top of all of the other navigation items. This is a simple example, but there are far more complex applications with this method. Imagine being able to sort files in a Folder asset by type using HTML::Expr (which is a topic for another day), or creating Collaboration Systems that can sort by the author of the post.

I've heard some concern in the past about the hit to the server by using a loop twice in an asset, and the fact is that unless there is a ridiculous amount of information for that asset, there is little to worry about. So by all means, experiment!

Stay tuned. Next month I'm going to cover using CSS as a substitute for ten of your daily vitamin requirements.

Re: Loop De Loop·
patspam · 2/15/2009 4:47 pm

Hey Steve,

Great to see the design squid posts flowing regularly. Keep up the great work.

One minor quibble, in the case of the attachment loop the true/false test is unnecessary because if the loop is empty the inner loop won't do anything. It's the equivalent of saying in perl:

if (@attachment_loop) {
    foreach (@attachment_loop) {
        # ...
    }
}

When you really only need to say:

foreach (@attachment_loop) {
    # ...
}

Cheers,

Patrick

Re: Loop De Loop·
preaction · 2/15/2009 6:39 pm

Alternatively you could get rid of the __FIRST__ and __LAST__ instead, leaving something like this:

<tmpl_if attachment_loop>
    <div class="attachments">
    <tmpl_loop attachment_loop>
        <div><a href=”<tmpl_var.page.url>”><tmpl_var page.title></a></div>
    </tmpl_loop>

    </div>
</tmpl_if>

Re: Loop De Loop·
Meatbop · 2/23/2009 11:53 am

You make a good point, pat. Because I used __FIRST__ and __LAST__ I wouldnt' have to have the loop test. Preaction's example is a better showcase of a solid reason to use that testing methodl.

·
Stick
Lock
Subscribe