简体   繁体   English

在树枝[OctoberCMS]中打印数组的输出值

[英]Print the output value of an array in twig [OctoberCMS]

I have an array of values in my component and I want to output it in the default.htm my component is like this: 我的组件中有一个值数组,我想在default.htm中输出它我的组件是这样的:

  $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: ogFbAdmins从后端的转发器字段获取值,该字段允许用户创建他想要的任意数量的输入框,我想在php中为每个输入框做一些事情,但是在twig上,如果我使用这个输出我的数组码:

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: 我的default.htm上的twig代码是这样的:

{% 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 我的这个twig代码返回一个错误

"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. 如果您不理解这个问题,请随时提出任何问题。

just change content="{{ad}}" to content="{{ad.str_og_fb_admins}}" it will make it work. 只需将content="{{ad}}"更改为content="{{ad.str_og_fb_admins}}"使其正常运行。

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 %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM