【问题标题】:October CMS | Creating a portfolio with 'show more' but struggling with twig十月CMS |创建一个“显示更多”但在树枝上苦苦挣扎的作品集
【发布时间】:2021-03-01 03:06:44
【问题描述】:

我想要实现的是,当超过 6 个项目时,剩余的项目会自动进入“显示更多”部分。

{% for item in portfolioItems %}
<div class="col-md-4">
    <img src="{{ item.portfolioItemsImage |media }}" alt="">
    <h5>{{item.portfolioItemsTitle}}</h5>
</div>

After 6 items have been displayed above the rest of the items should be shown below.

<!-- HIDDEN -->
<div id="showmore" class="collapse">
    <img src="{{item.portfolioItemsImage|media}}" alt="">
    <h5>{{item.portfolioItemsTitle}}</h5>
</div>
{% endfor %}

<button class="btn btn-primary" data-toggle="collapse" data-target="#showmore">Show More</button>

【问题讨论】:

    标签: html twig octobercms


    【解决方案1】:

    您可以创建两个循环并使用slicing techniques:

    {% for item in portfolioItems[:6] %}
      <div class="col-md-4">
        <img src="{{ item.portfolioItemsImage|media }}" alt="">
        <h5>{{ item.portfolioItemsTitle }}</h5>
      </div>
    {% endfor %}
    
    {% for item in portfolioItems[6:] %}
      <!-- HIDDEN -->
      <div id="showmore" class="collapse">
        <img src="{{ item.portfolioItemsImage|media }}" alt="">
        <h5>{{ item.portfolioItemsTitle }}</h5>
      </div>
    {% endfor %}
    

    这里有简单的演示:https://twigfiddle.com/6olcw2


    或者您可以使用loop variables 和if/else

    {% for item in portfolioItems %}
      {% if loop.index <= 6 %}
        <div class="col-md-4">
          <img src="{{ item.portfolioItemsImage|media }}" alt="">
          <h5>{{ item.portfolioItemsTitle }}</h5>
        </div>
      {% else %}
        <!-- HIDDEN -->
        <div id="showmore" class="collapse">
          <img src="{{ item.portfolioItemsImage|media }}" alt="">
          <h5>{{ item.portfolioItemsTitle }}</h5>
        </div>
      {% endif %}
    {% endfor %}
    

    这里有简单的演示:https://twigfiddle.com/6olcw2/2

    【讨论】:

      猜你喜欢
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 2013-02-20
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多