1

Is it possible for sqlalchemy python flask to break the loop and get the single result value only. for example on my case:

I am looping a list and will have multiple result but my aim is once I get the first row I will break now the loop.

   {% for item in cert %}
    <div>
        <label>ID No:</label>                     
        <label font size="6">{{item.empID}}</label>                                  
        <label class="pull-right">Total Certificates:</label>

    </div>
    <div>
        <label>Name:</label>   
        <label>{{item.name}}</label>                 
    </div>
    <div>
        <label>Department:</label>                    
        <label>{{item.dept}}</label>
    </div>
    <div>
        <label>Process:</label>                    
        <label>{{item.proc}}</label>
    </div>

    {% endfor %}

I just want to have something like in python:

list = [1,2,3]
for item in list:
    return i
    break 

Any suggestion or comments will appreciate. Thanks in advance!

Prune
  • 76,765
  • 14
  • 60
  • 81
Syntax Rommel
  • 932
  • 2
  • 16
  • 40

1 Answers1

0

Jinja2 doesn't have any default options to break from a loop in the template.

Instead you could just skip the loop all together and just break out the first element in the list with one of the following

{% set item = cert | first %}

{% set item = cert[0] %}

However, it would make more sense to modify the data you pass to the template so it only includes one row in the first place instead of a whole set.

Johan
  • 3,577
  • 1
  • 14
  • 28