4

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?

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

1 Answer 1

11

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
1
  • 1
    Thank you Ben. This is what I needed and did the trick perfectly! Commented Jun 26, 2014 at 18:03

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.