Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I need to get a list of all the "sizes" from my Table type field column. The list of sizes needs to be separated by a "|"... eg: small|medium|large

My Table type field name is "productAttributes" and the column in that field is "size".

How can I string together the results of a For Each loop to create a new variable with Twig?

share|improve this question
    
For what it's worth... Selvin taught me how to do this once but I can't remember! –  Anna_MediaGirl Jun 26 at 17:44
    
Do you want this in PHP or Twig? Is this a custom table for a plugin. –  Bryan Redeagle Jun 26 at 17:53
    
For Twig... This is data stored in a "Table" field type... not a custom plugin. –  Anna_MediaGirl Jun 26 at 18:01
    
might be worth clarifying this in the question so it is more useful for people viewing this in future... –  Ben Croker Jun 26 at 18:04
    
I edited my question. –  Anna_MediaGirl Jun 26 at 18:31

1 Answer 1

up vote 8 down vote accepted

If I understood your question correctly then you should be able to do it as follows:

{% set sizes = [] %}

{% for productAttribute in entry.productAttributes %}
    {% set sizes = sizes|merge([productAttribute.size]) %}
{% endfor %}

{% set sizes = sizes|join('|') %}

The sizes variable will then be a string containing the pipeline separated sizes, for example:

small|medium|large
share|improve this answer
    
Thank you Ben. This is what I needed and did the trick perfectly! –  Anna_MediaGirl Jun 26 at 18:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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