在Twig Symfony中计算键数目。

huangapple go评论54阅读模式
英文:

count key in twig symfony

问题

我尝试简单地计算城市中的对象

在Twig Symfony中计算键数目。

在巴黎,我有5个对象,我不想显示11111,只想显示5,

这是我的代码

{% set newAnnounceCity = [] %}

{% for item in announceCity %}
    {% if item.infoCity is not null %}
        {% if item.infoCity.city not in newAnnounceCity %}
            <span class="font-semibold mr-2 text-left flex-auto">
                <a href="{{path('app_city_show',{slug: item.infoCity.slug})}}">
                    <button class="m-2 p-2 pl-5 pr-5 bg-transparent border-2 border-red-500 text-red-500 text-lg rounded-lg hover:bg-gradient-to-b hover:from-red-600 hover:to-pink-500 hover:text-gray-100 focus:border-4 focus:border-red-300">
                        {{ (item.infoCity.city) }}
                        {% for key in item.infoCity.announce|keys %}
                            {{key|length }}
                        {% endfor %}
                        {% set newAnnounceCity = newAnnounceCity|merge([item.infoCity.city]) %}
                    </button>
                </a>
            </span>
        {% endif %}
    {% else %}
    {% endif %}
{% endfor %}

有人可以帮助我吗?

我尝试计算一个键的对象数

英文:

i try to simply count object into a city

在Twig Symfony中计算键数目。

in paris, i have 5 object, i don't want show 11111 but just 5,

that my code

{% set newAnnounceCity = [] %}


		{% for item in announceCity %}


			{% if item.infoCity is not null %}


				{% if item.infoCity.city not in newAnnounceCity  %}


					&lt;span class=&quot; font-semibold mr-2 text-left flex-auto&quot;&gt;
						&lt;a href=&quot;{{path(&#39;app_city_show&#39;,{slug: item.infoCity.slug})}}&quot;&gt;


							&lt;button class=&quot; m-2 p-2 pl-5 pr-5 bg-transparent border-2 border-red-500 text-red-500 text-lg rounded-lg hover:bg-gradient-to-b hover:from-red-600 hover:to-pink-500 hover:text-gray-100 focus:border-4 focus:border-red-300&quot;&gt;
								{{ (item.infoCity.city) }}

								{% for key in item.infoCity.announce|keys %}
									{{key|length }}
								{% endfor %}

								{% set newAnnounceCity = newAnnounceCity|merge([item.infoCity.city]) %}


							&lt;/button&gt;
						&lt;/a&gt;

					&lt;/span&gt;
				{% endif %}
				{% else %}

			{% endif %}

		{% endfor %}

someone can help me

i try to count an keys object

答案1

得分: 2

用简单的代码替换这部分可能会更容易,但要猜测没有提供输入数组的结构有点困难。您可以尝试将原代码:

{% for key in item.infoCity.announce|keys %}
    {{key|length }}
{% endfor %}

替换为:

{{ item.infoCity.announce|length }}
英文:

It's a bit hard to guess without a sample structure of your input array, but maybe try replacing

{% for key in item.infoCity.announce|keys %}
    {{key|length }}
{% endfor %}

with simply

{{ item.infoCity.announce|length }}

huangapple
  • 本文由 发表于 2023年2月18日 19:07:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定