Forum
perch:different inside perch:related
I'm using relationships for sorting purposes
<perch:related id="differential_brand" collection="Sort - Differential Brand" label="Differential Brand" size="m" divider-before="Sorting" ></perch:related>
<perch:related id="gear_size" collection="Sort - Gear Size" label="Gear Size" size="m"></perch:related>
<perch:related id="gear_type" collection="Sort - Gear Type" label="Gear Type" size="m"></perch:related>
to do stuff like
perch_collection('Ring & Pinion - Family',[
'filter' => 'differential_brand.slug',
'match' => 'eq',
'value' => perch_get('brand'),
'template'=>'_get/_rp-gear_sizes.html',
]);
so that I can create filtering links. I don't want duplicate filter links, so I'd like to use <perch:if different="slug">
to prevent duplicates:
_rp-gear_sizes.html
<perch:related id="gear_size" collection="Sort - Gear Size" label="Gear Size" size="m">
<perch:if different="slug">
<li><a class="icon-circle<perch:if id="slug" match="eq" value="{sort_curr_slug}"> active</perch:if>" data-id="<perch:content id="slug" type="slug" for="title" editable="true" indelible="true" label="Slug" />" href="<perch:content id="sort_curr_url" type="hidden" encode="false" /><perch:content id="slug" type="slug" for="title" editable="true" indelible="true" label="Slug" />"><perch:content id="title" type="text" label="Title" required="true" title="true" /></a></li>
</perch:if>
</perch:related>
However, it seems that perch:different doesn't work inside perch:related. It's outputting the same as without the <perch:if different="slug">
:
<ul>
<li><a class="icon-circle" data-id="8-8" href="/products?cat=ring-and-pinion&brand=ford&size=8-8">8.8"</a></li>
<li><a class="icon-circle active" data-id="8" href="/products?cat=ring-and-pinion&brand=ford&size=8">8"</a></li>
<li><a class="icon-circle" data-id="9" href="/products?cat=ring-and-pinion&brand=ford&size=9">9"</a></li>
<li><a class="icon-circle" data-id="9" href="/products?cat=ring-and-pinion&brand=ford&size=9">9"</a></li>
</ul>
Am I doing something wrong here, or is this just not possible? Right now I'm removing the duplicates with jQuery, but I'd like for them to not exist in the DOM in the first place, if possible.
In case you're wondering why I would need to do this, it's because the various filters are in a hierarchy. Types depend on size, sizes depend on brand:
Are they sorted? Different only compares to the previous item.
Do you mean sort the perch:related? I thought the only sort option for that was sort="custom". I added that, no change. Also tried:
No change, and it seems using the perch:related dot syntax for the sort ID doesn't do anything either, as changing the order from ASC to DESC made no change.
different
only looks at the proceeding item, it's not doing any sort of de-dupe. If the item before has a different value, it outputs.perch:related seems to output in alphabetical order, so they are in order. It's outputting items with the same slug in order, even with
different
in place.Ok, so it's sorted by slug so adjacent slugs are being output, but adding
if:different
has no effect?Yes, that's right. Is
different
supposed to work inside ofperch:related
?If I use
skip-template
and put the results in an array, can I compare the[gear_size]
array somehow, remove duplicates, and then output the templated results? These items will always be related to only one gear size, so if they have identical[gear_size]
arrays, then the duplicates can be removed. This is just beyond my PHP abilities. Here's what the array looks like. You can see the last two items have identical[gear_size]
arrays.Is not supposed to not work.
Yes, that would be one approach.
If anyone has any ideas what that might look like...I'm all ears. :)
That's beyond the scope of free support, but someone may wish to help.
Shane,
for each
Ring & Pinion - Family
item, how manygear_sizes
are assigned? From your skip-template result, it looks as if only one gear_size is selected for each item?Yes Duncan, that's correct. Each item will only have one gear size related.
I think that's where things aren't working - with the
different="slug"
inside theperch:related
tags, you would only be looking for differences if there were multiple gear_sizes selected for that one item.I don't know if you can move the
different=""
outside of the related tags and have something along the lines ofI think that's what you're expecting to happen - filter the number of items returned by whether the gear_size field is equal or not.
Duncan, you're absolutely right, I can't believe I didn't realize that! I don't think it's possible to compare the related content outside of
perch:related
, though. This isn't working:Doesn't output anything at all. I think the dot syntax only works in page functions. Drew, can you weigh in here to let us know if that should work? Would be great to be able to do this!
Scratch that, this works!
Thanks for the help!
Ah, yes, that would work!
I didn't think the dot syntax would work - it's down to fact that the related item could have multiple entries. Multiple entries would affect your solution too, but if you know it will only have one, you should be OK. You can also use
max="1"
to force it to only accept one entry.Good idea. Thanks again, Duncan!