Twig image gallery algorithm for bootstrap columns

2019-06-09 02:28发布

Hello I am trying to create a foto gallery using twig and bootstrap that has an algorithm for width. Example: I hope its clear every section is devided by a thicker black line.

enter image description here

So I tried allot of things out but now it seems im close to the solution but its still not working properly this is my code atm:

    <section>
        <div class="row row-no-gutter">
            {% setcontent miniaanbod = "aanbod" %}
            {% for items in miniaanbod|reverse %}

                {% set cols = 3 %}
                {% set totaal = miniaanbod|length %}

                {#  this is the beginning.  #}
                    {% if totaal - cols <=0 %}   {# if the total is less than 0 it means it is 1 or 2. so my colwidht would be or 6 or 12 #}

                        {% set colwidth = 12/totaal %} {# expected that totaal is 1 or 2 here #}

                    {% else %}

                        {% set totaal = totaal - cols %} {# expected that if the totaal is 4 it takes of 3 and goes back to the begining with 1. so the first IF statement can catch it. #}
                        {% set colwidth = 12/3 %} {# expected that totaal is 3 here so most likely I can just do 12/3 so I did that #}

                    {% endif %}

                {#{{ dump(colwidth) }}#}
                <div class="col-sm-6 col-md-{{ colwidth }}">
                    <div class="product">
                        <img src="{{ image(items.gallerij[0].filename, 1620, 880) }}" alt="">
                        <div class="product__cnt">
                            <h5 class="uppercase">{{ items.type }}</h5>
                            <ul class="pricing-table">
                                <li>Prijs : <span class="price">{{ items.prijs }}</span></li>
                            </ul>
                            <span class="link"><a href="/aanbod/{{ items.slug }}">Continue</a></span>
                        </div>
                    </div>
                </div>
            {% endfor %}
        </div>
    </section>

what this does it changes all the pictures to col-md-4 but what it should do is after every 3 items it has to return to the start of the if statement. but I cant seem to get it to the beginning again. I also tried to put another for loop inside the other for loop to get it to return to the beginning after every item but then it always creates col-md-12 becouse it only has one item at a time.

1条回答
贼婆χ
2楼-- · 2019-06-09 02:48

After 2 hours of more try and error. I finally created the solution.

    <section>
        <div class="row row-no-gutter">
            {% setcontent miniaanbod = "aanbod" %}

                {% set teller4 = 0 %}
                {% set teller6 = 0 %}

                {% set breedte = 0 %}
                {% set alles = miniaanbod|length %}
                {% for item in miniaanbod|reverse %}
                    {% if (alles >= 3 ) or (teller4 is not divisible by(3)) %}
                        {% set breedte = 4 %}
                        {% set teller4 = teller4 + 1 %}
                        {% set alles = alles - 1 %}
                    {% elseif (alles > 1) or (teller6 is not divisible by(2)) %}
                        {% set breedte = 6 %}
                        {% set teller6 = teller6 + 1 %}
                        {% set alles = alles - 1 %}
                    {% else %}
                        {% set breedte = 12 %}
                        {% set alles = alles - 1 %}
                    {% endif %}

                    <div class="col-sm-6 col-md-{{ breedte }}">
                        <div class="product">
                            <img src="{{ image(item.gallerij[0].filename, 1620, 880) }}" alt="">
                            <div class="product__cnt">
                                <h5 class="uppercase">{{ item.type }}</h5>
                                <ul class="pricing-table">
                                    <li>Prijs : <span class="price">{{ item.prijs }}</span></li>
                                </ul>
                                <span class="link"><a href="/aanbod/{{ item.slug }}">Continue</a></span>
                            </div>
                        </div>
                    </div>
                {% endfor %}
        </div>
    </section>

this works perfectly and it has been tested. I hope this is ussefull to someone else. later onn

查看更多
登录 后发表回答