I have two levels of product variants in a table field. If the product uses Secondary Variants, I need a way to search the Primary Variants column for unique values, then output individually grouped Secondary variants.

Both Primary and Secondary Variants are unique for each product.

Is there a way to do this with TWIG/Craft templates? If not, what do you recommend? Query JSON? Plug-in?

  • Ben, any success using my example code?
    – carlcs
    Commented Dec 9, 2014 at 1:45
  • Apologies for the delay. It had to do with my schedule, not your answer.
    – Ben
    Commented Dec 9, 2014 at 18:39
  • No need to apologize, Ben! Just wanted to know if you still use the table or restructured the setup to use some related entries instead. Updated the post :)
    – carlcs
    Commented Dec 9, 2014 at 18:45
  • I'm still using the table for client ease-of-use (as you surmised).
    – Ben
    Commented Dec 9, 2014 at 18:57

1 Answer 1


Possible, yes. But if you should do it with Twig alone is a totally different thing. There's other ways to do that in Craft, e.g. with relations (Entries Fields) and splitting your Products and their variants into separate sections. Or what about using a structure, with your variants being child entries to the product?

But here's how you do that with your table field (totally legitimate in my opinion, one advantage being the ease of use for the Client):

{# Prepare variable before loop #}
{% set myVariants = [] %}

{% for row in myTableField %}

    {# Concatenate a string to the value to prevent it from being numeric #}
    {% set variantKey = 'key' ~ row.col1 %}

    {% if variantKey not in myVariants|keys %}

        {# Insert a new key and add the first value to it #}
        {% set myVariants = myVariants|merge({ (variantKey): [row.col2] }) %}

    {% else %}

        {# The key already exists, so merge the value to the existing ones #}
        {% set myVariants = myVariants|merge({ (variantKey): myVariants[variantKey]|merge([row.col2]) }) %}

    {% endif %}

{% endfor %}

This is how you print it:

<ul class="variantsLevel1">
    {% for variantKey, variantsLevel1 in myVariants %}

            {# Remove the string from our key again #}
            {{ variantKey|slice(3) }}

            <ul class="variantsLevel2">
                {% for variantLevel2 in variantsLevel1 %}
                    <li>{{ variantLevel2 }}</li>
                {% endfor %}

    {% endfor %}
  • Thanks so much for the response. The only problem I found with using this answer was that by using the word 'key' for the key, it truncated any variantLevel1 names that ended with k,e or y. I'm sure there is a way to force it to only look for that sequence of letters if you would like to edit the answer.
    – Ben
    Commented Dec 9, 2014 at 18:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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