1

I have a website where I want to show Power Coat (-PC) options at the end of a list. The data goes into Craft in an unpredictable order, from a third-party service, so I am responsible for organizing it in the front-end.

Is there a way I can split out or force the -PC items to always appear at the end of the list, in my template?

enter image description here

1

How would you like to identify that particular entry? If you're happy to hardcode its id, then I'd do it like this with the filter filter and the without filter:

{% set allEntries = craft.entries.section('products').all() %}
{% set pcEntry = allEntries|filter(item => item.id = 1234) %}
{% set entriesWithoutPC = allEntries|without(pcEntry) %}

You can now output those however/wherever you like, e.g.

<ul>
  {% for product in entriesWithoutPC %}
    <li>{{ product.title }}</li>
  {% endfor %}
  <li>{{ pcEntry[0].title }}</li>
</ul>
| improve this answer | |
1

James’s answer is correct, if there's only one list of items. I'm going to propose a derivative solution, in case you're dealing with an abstract set of elements (like a common pattern across many Products’ Variants).

{% set finishes = product.variants %}
{% set powderCoatedSuffix = 'PC' %}

<ul>
  {# Output the variants that *don't* end with `PC`: #}
  {% for variant in variants | filter(v => v.sku[-2:] != powderCoatedSuffix) %}
    <li>{{ variant.title }}: {{ variant.sku }}</li>
  {% endfor %}

  {# Then, output those that *do*: #}
  {% for variant in variants | filter(v => v.sku[-2:] == powderCoatedSuffix) %}
    <li>{{ variant.title }}: {{ variant.sku }}</li>
  {% endfor %}
</ul>

The only trickery here is the short syntax for the slice filter—in this case, taking the last two characters of the string.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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