0

I am trying to render all the elements in my array when there is an element there and when there isn't. When there isnt an element at that position I want to display that there isnt an exercise(element) at that position. This requires a short bit of code which works in python but not in Jinja. Which doesn't make sense to me unless I am missing a difference between the two languages.

The reason I wrote out the code in python first is because I have been trying for a while to get this working. I thought if I wrote the python I could translate it but I guess not?

Working python code:

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")

Not working Jinja code:

{% 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 prints this:

1                                                                      
Exercise 2                                                             
2                                                                      
no exercise                                                            
3                                                                      
Single leg balance abd/adduction 

Jinja renders this:

Exercise 2
no exercise
Day 2
no exercise
Day 3
Single leg balance abd/adduction
no exercise
Dan
  • 53
  • 1
  • 7

1 Answers1

1

In general, while it’s possible to create complex branching logic in Jinja, it’s usually the wrong place for it. Beyond the visual clutter of the {% %}, there’s just the fact that the template is for client-side code as much as possible, not business logic.

My first approach here would be to process the array in Python, creating a new one with eg None included for a missing exercise. Then simply loop through the new array in Jinja, checking for None, and render the appropriate HTML. If you need access to the index of the current item, you can use the loop.index and loop.index0 variables.

Nick K9
  • 3,885
  • 1
  • 29
  • 62
  • A [view](https://flask.palletsprojects.com/en/latest/api/?highlight=view#flask.views.View) in Flask is the code that responds to an incoming request after it's routed. It does not use the same terminology as other frameworks. You also don't *print* anything in a template; values are *rendered.* – jpmc26 Sep 12 '19 at 20:30
  • Thanks. I was using ‘view’ as in MVC, but even that isn’t quite the right division. I’ve adjusted the wording. – Nick K9 Sep 13 '19 at 07:22