1

With FeedMe I'm importing some data and one of them I get is a complete array that is stored a plain text field.

This array is a bunch of car options what I want to add in a table like this:

{% set options = entry.optionsAndAccessoiries %}

output {{ options }} = ["Achterbank in delen neerklapbaar","Airbag bestuurder","Airconditioning","Anti-slipregeling","Antiblokkeersysteem","Centrale deurvergrendeling afstandbediend","Elektrisch bedienbare ramen voor","Elektrisch verstelbare buitenspiegels","Getint glas","Hoofdsteunen achter","Hoofdsteunen voor","In hoogte verstelbare bestuurdersstoel","Metallic lak","Middenarmsteun voor","Mistlampen voor","Radio\/CD speler","Startonderbreker","Stoelverwarming bestuurder","Stoffen bekleding","Stuurbekrachtiging","Trekhaak","Verstelbaar stuurwiel"]

{% set items = options %}

 <table>
  {% for row in items|batch(3, 'No item') %}
  <tr>
    {% for column in row %}
    <td>{{ column }}</td>
    {% endfor %}
  </tr>
  {% endfor %}
</table>

But this is causing an error:

array_chunk() expects parameter 1 to be array, string given

Who can help me out to point me in the right direction?

  • When you {{ options }} you literally get the output that you displayed above? i.e. square brackets and everything? – Brad Bell Dec 4 '16 at 2:15
  • Yes correct. And when I copy/paste that to {% set options = ["Achterbank in delen neerklapbaar","Airbag bestuurder","etc..."] it works like a charm. – John Stevens Dec 4 '16 at 9:16
  • This is not working: {% set opties = ["Achterbank in delen neerklapbaar","Airbag bestuurder","Airconditioning"] %} {% set items = opties %} <table> {% for row in items|batch(3, 'No item') %} <tr> {% for column in row %} <td>{{ column }}</td> {% endfor %} </tr> {% endfor %} </table> – John Stevens Dec 4 '16 at 9:43
  • While this is working: {% set items = ["Achterbank in delen neerklapbaar","Airbag bestuurder","Airconditioning"] %} <table> {% for row in items|batch(3, 'No item') %} <tr> {% for column in row %} <td>{{ column }}</td> {% endfor %} </tr> {% endfor %} </table> – John Stevens Dec 4 '16 at 9:45
2

Sounds like your options simply need to be JSON decoded. Unfortunately, Twig doesn't ship with a json_decode filter, so you'll need a plugin – there are several that fit the bill over at StraightUpCraft.

Using the JSON Decode Filter plugin, here's how your code would look:

{% set options = entry.optionsAndAccessoiries|json_decode %}
...

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.