php - 每次迭代树枝循环时减去一个
问题描述
我必须在 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 制作的,所以我需要第二个循环在每次迭代时少显示一颗星星。
解决方案
您需要在for
-loops 中使用一个变量来跟踪最大数量的星星。
<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>
推荐阅读
- docker - 是否可以在容器内获得指定的 docker ram 限制?
- python - tensorflow - tf.data.Dataset 在批处理之前随机跳过样本以获得不同的批次
- confluence - 使用变量并在内容中显示
- java - 带有背景加载的imageview中的Javafx中心图像
- javascript - 如何在网站上的 github 上获取最新提交
- azure - 推荐实践如何在 Azure Function local.settings.json 文件中包含更复杂的配置 JSON 数据?
- unity3d - Vuforia 无法识别我显示的图像。Unity 3D 2018 最新版本
- python - 用字符串python中的正则表达式替换正则表达式
- reactjs - ReactJS:如果我单击浏览器,则读取未定义的属性“地图”
- python - 如何使用python将通用或任何json解析为csv