Reputation: 3502
I have the sample mongo document data as (for ref):
from view as:
data=wordscollection.find({'word':word})
return render_template('wordsearch.html',data=data)
In the template I have this just for the first index and doesn't check if example or synonym is empty:
{% for word in data %}
<tr>Meaning :{{ word['meanings'][0]['def'] }}</tr><br>
<tr>Example :{{ word['meanings'][0]['example'] }}</tr><br>
<tr>Parts Of Speech :{{ word['meanings'][0]['speech_part'] }}</tr>
{% endfor %}
which outputs as:
1) How can I display all indexes results as
meaning1: def1
speechpart1: speech_part1
example1: //2)writing condition for this to show if exits
synonym: //2)writing condition for this to show if exits
Meaning2: def2
........
........
I have tried my luck for 2) as below :
{% for word in data if word['meanings'][0]['example'] %}
but this didn't work either
Any help is appreciated, TIA
Upvotes: 0
Views: 47
Reputation: 3502
updated answer for synonyms
{% for word in data %}
{% for meaning in word['meanings'] %}
<tr>Meaning :{{ meaning['def'] }}</tr><br>
{% if meaning['example'] %}
<tr>Example :{{ meaning['example'] }}</tr><br>
{% endif %}
{% if meaning['speech_part'] %}
<tr>Parts Of Speech :{{ meaning['speech_part'] }}</tr><br>
{% endif %}
{% for synonyms in meaning['synonyms'] %}
{% if meaning['synonyms'] %}
<tr>synonyms : {{ synonyms }} </tr><br>
{% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
Upvotes: 0
Reputation: 1409
You have to use nested for loop to iterate meanings
also and check for empty values before
{% for word in data %}
{% for meaning in word['meanings'] %}
<tr>Meaning :{{ meaning['def'] }}</tr><br>
{% if meaning['example'] %}
<tr>Example :{{ meaning['example'] }}</tr><br>
{% endif %}
{% if meaning['speech_part'] %}
<tr>Parts Of Speech :{{ meaning['speech_part'] }}</tr>
{% endif %}
{% endfor %}
{% endfor %}
Upvotes: 1