当有元素和没有元素时,我试图渲染数组中的所有元素。当该位置没有元素时,我想显示该位置没有练习(元素)。这需要一小段代码,它在 python 中有效,但在 Jinja 中无效。除非我错过了两种语言之间的区别,否则这对我来说没有意义。
我首先在 python 中编写代码的原因是因为我已经尝试了一段时间来让它工作。我想如果我写了python,我可以翻译它,但我猜不是?
工作python代码:
dailyExercise = [('Exercise 1', 1), ('Exercise 2', 3)]
for x in range(dailyExercise[-1][-1]):
print(x+1)
isExercise = False
for exercise in dailyExercise:
if exercise[-1] == x+1:
print(exercise[0])
isExercise = True
break;
if isExercise == False:
print("no exercise")
不工作的神社代码:
{% for x in range(dailyExercise[-1][-1]) %}
<p>Day {{x+1}}</p>
{%set isExercise = False%}
{% for exercise in dailyExercise%}
{% if exercise[-1] == x+1 %}
{%set isExercise = True%}
<p>{{exercise[0]}}</p>
{% endif %}
{% endfor %}
{% if isExercise == False%}
<p>no exercise</p>
{% endif %}
{% endfor %}
Python 打印:
1
Exercise 2
2
no exercise
3
Single leg balance abd/adduction
Jinja 渲染了这个:
Exercise 2
no exercise
Day 2
no exercise
Day 3
Single leg balance abd/adduction
no exercise