1

Trying to see if there is a way natively in EE (2.7.3) not just to switch classes within a ul but to have multiple instances of ul tags within one channel entry without using add-ons like switch?

<ul class="sub">
<li>A</li>
<li>B</li>
<li>C</li>
</ul>

<ul class="sub">
<li>D</li>
<li>E</li>
<li>F</li>
</ul>

2 Answers 2

1

Now that ExpressionEngine includes a modulus operator (as of 2.7.1), you can also use that instead of switch if you prefer.

Here are a couple of examples that both accomplish the same result.

Using modulus to conditionally output all opening and closing html tags:

{exp:channel:entries channel="news"}

    {if count % 3 == 1}
        <ul class="sub">                
    {/if}

    <li>{title}</li>

    {if count % 3 == 0 OR count == total_results}
        </ul>
    {/if}

{/exp:channel:entries}

...OR....

Put the first opening tag and the final closing tag outside of channel:entries and use modulus only for conditional closing and re-opening of blocks of html:

<ul class="sub">                
    {exp:channel:entries channel="news"}

        <li>{title}</li>

        {if count % 3 == 0}
            </ul>
            <ul class="sub">
        {/if}

    {/exp:channel:entries}
</ul>

Either of these examples will work and will always ensure that you have proper tag closure. It's just a matter of preference.

EE Documentation on Modulus Operator

2
  • Thank you. We deal with display issues showing columns and rows all the time and with the modulus operator this looks like it'll be much easier.
    – jsuissa
    Dec 17, 2013 at 1:43
  • You're welcome. I was happy EllisLab added modulus, as it's a much cleaner way to handle conditional html output. Feel free to change which answer you accept. ;)
    – Alex Kendrick
    Dec 17, 2013 at 3:21
2

The switch variable takes html as well. In case of your example:

{switch='<ul class="sub">||'}
<li>{title}</li>
{switch='||</ul>'}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.