Forum
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:
- if the user inputs a subhead or preamble:
- if this is not the first item in the repeater, a
</ul>
is output, closing any previously-started lists - if they input a subhead, it is output as an
<h4>
- the preamble is output
<ul>
is output, starting a new list
- if this is not the first item in the repeater, a
- if the user didn't input a subhead or preamble:
- if this is the first item in the repeater, a
<ul>
is out, starting a new list
- if this is the first item in the repeater, a
- an
<li>
is output, starting a new list item - the text is output
- if the user input a level, the level is output
- 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?
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 ofelse
. To do this, I duplicated theelse condition
block in theparse_every
function inperch/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:Note that this is a fairly hacky solution that will break whenever Perch gets updated.
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
There's a good example as part of Members, but I do need to find time to write it up!