Forum

Thread tagged as: Problem

nested conditionals

I have a situation where I'm using a repeater to generate <li>s in a <ul>, and I want to end and restart the <ul> if the user fills out certain fields in the repeater.

So…

<perch:if exists="recs">
    <perch:repeater id="recs">
        <perch:if exists="rec_subhead OR rec_preamble">
            <perch:every nth-child="1">
            <perch:else />
                </ul>
            </perch:every>
            <perch:if exists="rec_subhead">
                <h4><perch:content id="rec_subhead" type="text" … /></h4>
            </perch:if>
            <perch:content id="rec_preamble" type="textarea" … />
            <ul>
        <perch:else />
            <perch:every nth-child="1">
                <ul>
            </perch>
        </perch:if>
        <li>
            <perch:content id="rec_text" type="textarea" … />
            <perch:if exists="rec_level">
                <perch:content id="rec_level" type="text" … />
            </perch:if>
        </li>
    </perch:repeater>
</perch:if>

The way I think this should work is that, for each item in the repeater:

  1. if the user inputs a subhead or preamble:
    1. if this is not the first item in the repeater, a </ul> is output, closing any previously-started lists
    2. if they input a subhead, it is output as an <h4>
    3. the preamble is output
    4. <ul> is output, starting a new list
  2. if the user didn't input a subhead or preamble:
    1. if this is the first item in the repeater, a <ul> is out, starting a new list
  3. an <li> is output, starting a new list item
  4. the text is output
  5. if the user input a level, the level is output
  6. an </li> is output, closing the list item

This is admittedly complex and, unfortunately, I'm not getting the results I'd expect. I suspect there is a problem with nested conditionals; specifically, I suspect that the <perch:else /> that I want to be associated with <perch:every> is instead being associated with one of my <perch:if>s.

Should this type of nesting be working? Is there any way to achieve what I'm trying to do here?

David Newton

David Newton 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

That sounds likely. IF is parsed before EVERY.

Ok, thanks. I suppose that means I can't really do what I'm trying to, right?

In case anybody else has this issue:

I solved this by creating a new everyelse template tag to use instead of else. To do this, I duplicated the else condition block in the parse_every function in perch/core/lib/PerchTemplate.class.php (currently starting at line 885), replacing both instances of “perch:else” with “perch:everyelse”. Then it was just a matter of changing my template to use the new template tag:

<perch:if>
    <perch:every>
    <perch:everyelse>
    </perch:every>
<perch:else>
</perch:if>

Note that this is a fairly hacky solution that will break whenever Perch gets updated.

Drew McLellan

Drew McLellan 2638 points
Perch Support

The correct way would be to implement a Template Handler.

We can't offer support to anyone who hacks at the product.

Of course, you're right. I needed a temporary solution fast while I looked for a supported one, and that hack was my fast/temporary fix. I apologize if it was inappropriate to post it here; feel free to edit/remove that post so as not to lead others astray.

Regarding Template Handlers: a search of the documentation and the forum didn't turn anything up. Is there a resource you can point me to that explains these? I'd love to get a legit solution working.

Thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

There's a good example as part of Members, but I do need to find time to write it up!