Forum

Thread tagged as: Question

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.

Shane Lenzen

Shane Lenzen 18 points

  • 4 years ago

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:

                <div class="prodCat">
                    <?php if (perch_get('cat') !== 'ring-and-pinion') {echo '<a href="/products?cat=ring-and-pinion">';} ?><h5 class="icon-chevron-right">Ring & Pinion</h5><?php if (perch_get('cat') !== 'ring-and-pinion') {echo '</a>';} ?>
                    <?php if (perch_get('cat') === 'ring-and-pinion') { ?>
                    <div>
                        <div class="sort">
                            <span>Differential Brand</span>
                            <ul><?php 
                                PerchSystem::set_var('sort_curr_url', '/products?cat=ring-and-pinion&brand='); 
                                if (perch_get('brand')) { PerchSystem::set_var('sort_curr_slug', perch_get('brand')); }
                                perch_collection('Sort - Differential Brand'); 
                            ?></ul>
                        </div>
                        <?php if (perch_get('brand')) { ?>
                        <div class="sort">
                            <span>Gear Size</span>
                            <ul><?php 
                                PerchSystem::set_var('sort_curr_url', '/products?cat=ring-and-pinion&brand='.perch_get('brand').'&size='); 
                                if (perch_get('size')) { PerchSystem::set_var('sort_curr_slug', perch_get('size')); }
                                perch_collection('Ring & Pinion - Family',[
                                    'filter' => 'differential_brand.slug',
                                  'match'  => 'eq',
                                  'value'  => perch_get('brand'),
                                    'template'=>'_get/_rp-gear_sizes.html',
                                ]); 
                            ?></ul>
                        </div>
                        <?php if (perch_get('size')) { ?>
                        <div class="sort">
                            <span>Gear Type</span>
                            <ul><?php 
                                PerchSystem::set_var('sort_curr_url', '/products?cat=ring-and-pinion&brand='.perch_get('brand').'&size='.perch_get('size').'&type='); 
                                if (perch_get('type')) { PerchSystem::set_var('sort_curr_slug', perch_get('type')); }
                                perch_collection('Ring & Pinion - Family',[
                                    'filter' => [
                                        [
                                            'filter' => 'differential_brand.slug',
                                          'match'  => 'eq',
                                          'value'  => perch_get('brand'),
                                        ],
                                        [
                                            'filter' => 'gear_size.slug',
                                          'match'  => 'eq',
                                          'value'  => perch_get('size'),
                                        ],
                                    ],
                                    'template'=>'_get/_rp-gear_types.html',
                                ]); 
                                ?></ul>
                        </div>
                        <?php } } ?>
                    </div>
                    <?php } ?>
                </div>
Drew McLellan

Drew McLellan 2638 points
Perch Support

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:

                                perch_collection('Ring & Pinion - Family',[
                                    'filter'=>'differential_brand.slug',
                                  'match'=>'eq',
                                  'value'=>perch_get('brand'),
                                    'template'=>'_get/_rp-gear_sizes.html',
                                    'sort'=>'differential_brand.slug',
                                    'sort-order'=>'ASC',
                                ]); 

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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.

Drew McLellan

Drew McLellan 2638 points
Perch Support

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 of perch: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.

Array
(
    [0] => Array
        (
            [_id] => 68
            [title] => Ford 8.8"
            [_title] => Ford 8.8"
            [slug] => ford-8-8
            [ring_gear_diameter] => 
            [ring_gear_bolts] => 12 - 7/16"X20 RH
            [cover_bolts] => 10
            [carrier_breaks] => N/A
            [pinion_shaft_diameter] => 1.625"
            [spline] => 28
            [differential_brand] => Array
                (
                    [0] => 26
                )

            [gear_size] => Array
                (
                    [0] => 34
                )

            [gear_type] => Array
                (
                    [0] => 41
                )

            [_sortvalue] => 1002
        )

    [1] => Array
        (
            [_id] => 67
            [title] => Ford 8”
            [_title] => Ford 8”
            [slug] => ford-8
            [ring_gear_diameter] => 8.0"
            [ring_gear_bolts] => 10 - 7/16"X20 RH
            [cover_bolts] => N/A
            [carrier_breaks] => N/A
            [pinion_shaft_diameter] => 1.88"
            [spline] => 25
            [differential_brand] => Array
                (
                    [0] => 26
                )

            [gear_size] => Array
                (
                    [0] => 31
                )

            [gear_type] => Array
                (
                    [0] => 41
                )

            [_sortvalue] => 1003
        )

    [2] => Array
        (
            [_id] => 43
            [title] => Ford 9"
            [_title] => Ford 9"
            [slug] => ford-9
            [ring_gear_diameter] => 9.0"
            [ring_gear_bolts] => 10 - 7/16"X20 RH
            [cover_bolts] => N/A
            [carrier_breaks] => N/A
            [pinion_shaft_diameter] => 1.312"
            [spline] => 28
            [differential_brand] => Array
                (
                    [0] => 26
                )

            [gear_size] => Array
                (
                    [0] => 28
                )

            [gear_type] => Array
                (
                    [0] => 41
                )

            [_sortvalue] => 1004
        )

    [3] => Array
        (
            [_id] => 44
            [title] => Ford 9" PRO
            [_title] => Ford 9" PRO
            [slug] => ford-9-pro
            [ring_gear_diameter] => 9.0"
            [ring_gear_bolts] => 10 - 7/16"X20 RH - 10 - 1/2"X20 RH
            [cover_bolts] => N/A
            [carrier_breaks] => N/A
            [pinion_shaft_diameter] => 1.312" / 1.5"
            [spline] => 28 / 35
            [differential_brand] => Array
                (
                    [0] => 26
                )

            [gear_size] => Array
                (
                    [0] => 28
                )

            [gear_type] => Array
                (
                    [0] => 42
                )

            [_sortvalue] => 1005
        )

)
Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's right. Is different supposed to work inside of perch:related?

Is not supposed to not work.

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?

Yes, that would be one approach.

If anyone has any ideas what that might look like...I'm all ears. :)

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's beyond the scope of free support, but someone may wish to help.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Shane,

for each Ring & Pinion - Family item, how many gear_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.

Duncan Revell

Duncan Revell 78 points
Registered Developer

I think that's where things aren't working - with the different="slug" inside the perch: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 of

different="gear_size.slug"

I 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:

<perch:if different="gear_size.slug"><perch:related id="gear_size" collection="Sort - Gear Size" label="Gear Size" size="m"><perch:template path="content/sort/sort_cat.html" /></perch:related></perch:if>

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!

<perch:if different="gear_size"><perch:related id="gear_size" collection="Sort - Gear Size" label="Gear Size" size="m"><perch:template path="content/sort/sort_cat.html" /></perch:related></perch:if>

Thanks for the help!

Duncan Revell

Duncan Revell 78 points
Registered Developer

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!