每次迭代树枝循环时都减去一个

时间:2019-01-21 14:18:39

标签: php css twig

我必须在树枝上列出一个评级星星的列表,但是我没有找到每次迭代循环时都减去一的方法,这样列表中就少了一颗星星。

我有以下代码:

<ul>
    {% for input in 0..3 %}
        <li>
            <input type="radio" name="stars" />
            <label>
                {% for i in 0..4 %}
                    <span></span>
                {% endfor %}
                {% if input >= 1 %} <span class="stars">and more</span>{% endif %}
            </label>
        </li>
    {% endfor %}
</ul>

星星是用CSS制作的,因此我需要第二个循环,以便每次迭代时少显示一颗星星。

1 个答案:

答案 0 :(得分:0)

您需要在for循环中使用变量,以跟踪最大数量的恒星。

<ul>
    {% set j = 5 %}{# max amount of stars #}
    {% for input in 1..j %}{# this will be 5 all the time, because it's allready interpreted #}
        <li>
            <input type="radio" name="stars" />
            <label>
                {% for i in 1..j %}
                    <span></span>
                {% endfor %}
                {% if input >= 1 %} <span class="stars">y más</span>{% endif %}
            </label>
        </li>
        {% set j = j - 1 %}{# lower the amount of stars that will be shown next iteration #}
    {% endfor %}
</ul>

demo