我显示类别的内容,并且如果我还有其他内容,但按钮显示为“显示更多”,则该按钮已根据类别中的广告数量进行了标记,并且将按钮粘贴到“用于”之外即使类别内容为空,我也总是得到此按钮
<div class="pedagogical pedagogical--category js-pedagogical-items" data-type="category" data-nbdisplayed="4">
{% for categoryId in questionCategories %}
<div class="row pedagogical__items" data-type="category" data-value="{{ categoryId }}">
{% for tool in tools if tool.questionCategory == categoryId %}
<div class="col-12 col-md-6 pedagogical__item__wrapper">
{% include 'components/tool-preview-item.html.twig' with {'tool' : tool} %}
</div>
<div class="col-12 text-center">
<button class="btn btn-outline-secondary js-show-more" data-type="category" data-value="{{ categoryId }}">{{ 'show-more'|trans({}, 'action') }}</button>
</div>
{% endfor %}
</div>
{% endfor %}
</div>
答案 0 :(得分:1)
我认为您只需要在呈现按钮之前添加一个检查即可:
<div class="pedagogical pedagogical--category js-pedagogical-items" data-type="category" data-nbdisplayed="4">
{% for categoryId in questionCategories %}
<div class="row pedagogical__items" data-type="category" data-value="{{ categoryId }}">
{% set has_items = 'false' %}
{% for tool in tools if tool.questionCategory == categoryId %}
{% set has_items = 'true' %}
<div class="col-12 col-md-6 pedagogical__item__wrapper">
{% include 'components/tool-preview-item.html.twig' with {'tool' : tool} %}
</div>
{% endfor %}
{% if has_items == 'true' %}
<div class="col-12 text-center">
<button class="btn btn-outline-secondary js-show-more" data-type="category" data-value="{{ categoryId }}">{{ 'show-more'|trans({}, 'action') }}</button>
</div>
{% endif %}
</div>
{% endfor %}
</div>