Published by Mijingo

tips icon image

EE Insider Tips
Sponsored by Mijingo's EE 2 Screencasts

ExpressionEngine mini-howtos created by the EE Insider community.

Make sure to format/code your dynamic lists properly

  • Posted by JonnyUK
  • June 08, 2009

This is a very basic ‘mistake’ I often see in various code snippets involving dynamic lists.

In cases where you want to dynamically fill up a list, often people use code similar to the following:

{exp:weblog:entries weblog="your_weblog" dynamic="off"}

The problem occurs when there are actually no results/entries returned for whatever reason. This then leaves you with an empty and incorrectly formatted list, such as:


Because lists are supposed to contain list items, not only would this not validate against standards, but it would also look differently across the various browsers because some browsers will decide to display spacing where the list items should be (leaving blank gaps/spacing in your design where there shouldn’t be), while others simply won’t display anything. Some browsers will also have trouble with all the elements following the list (as they will treat them as though they are inside the list) and you may end up with quite an odd looking page and some very strange bugs that just don’t make sense.

The best way to achieve the same result would be to actually place your UL tags inside the weblog entries loop, but only display them on the first and last entry, as shown below:

{exp:weblog:entries weblog="your_weblog" dynamic="off"}
"{count}" == "1"}<ul>{/if}
{if "{count}" == "{total_results}"}</ul>{/if}

Therefore, if there are no entries, the

    tags will not be outputted leaving your template looking how it should be and valid/standards-compliant!

    Again, this is a very simple tip that many people will consider to be obvious, but that said I know I’ve come across plenty of examples before which have done this the wrong way.

    Hambo19:50 on 06.08.2009

    Good tip and I was in fact going to write up something similar!

    You can drop the quotes and curly braces around count and the number you are comparing to -

    {if count == 1} etc 

    This has given me a good idea for another tip!

    JonnyUK12:37 on 06.09.2009

    Nice to see others are finding this helpful.

    Thanks for the comment. Yeah, for some reason I add quotes around as a bit of a habbit, find it a bit easier to read I guess - good job EE’s templating is flexible enough to handle either way!