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?

  • For what it's worth... Selvin taught me how to do this once but I can't remember! – Anna_MediaGirl Jun 26 '14 at 17:44
  • Do you want this in PHP or Twig? Is this a custom table for a plugin. – Bryan Redeagle Jun 26 '14 at 17:53
  • For Twig... This is data stored in a "Table" field type... not a custom plugin. – Anna_MediaGirl Jun 26 '14 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 '14 at 18:04
  • I edited my question. – Anna_MediaGirl Jun 26 '14 at 18:31

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:

  • 1
    Thank you Ben. This is what I needed and did the trick perfectly! – Anna_MediaGirl Jun 26 '14 at 18:03

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.