Ad

Print The Output Value Of An Array In Twig [OctoberCMS]

- 1 answer

I have an array of values in my component and I want to output it in the default.htm my component is like this:

  $settings = Settings::instance();
  $this->ogFbAdmins = $settings->str_og_fb_admins;

The ogFbAdmins is getting the values from an repeater field at the backend that field allows the user to create as many input boxes as he wants, I want to do something like a for each in php but on twig, if I output my array using this code:

foreach ($this->ogFbAdmins as $adm) {
              echo $adm['str_og_fb_admins'];
            }

it returns this:

admin1admin2

my twig code at my default.htm is like this:

{% if __SELF__.ogFbAdmins == true %}
{% for ad in __SELF__.ogFbAdmins %}
{% if ad|length %}
  <meta property="fb:admins" content="{{ ad }}" />
{% endif %}
{% endfor %}
{% endif %}

My this twig code returns an error

"An exception has been thrown during the rendering of a template ("Array to string conversion")."

In the end what I want it to return is this:

<meta property="fb:admins" content="admin1" />
<meta property="fb:admins" content="admin2" />

Feel free to ask anything if you don't understand the question.

Ad

Answer

just change content="{{ad}}" to content="{{ad.str_og_fb_admins}}" it will make it work.

the twig code will be like this:

{% if __SELF__.ogFbAdmins == true %}
{% for ad in __SELF__.ogFbAdmins %}
{% if ad.str_og_fb_admins|length %}
  <meta property="fb:admins" content="{{ ad.str_og_fb_admins }}" />
{% endif %}
{% endfor %}
{% endif %}
Ad
source: stackoverflow.com
Ad