Reputation: 431
I use the django framework to do my task, I want to optimize my code in the template file. Here is my code blow.
{% ifequal name 'jack' %}
{% ifequal item.jack 'lower' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
{% endifequal %}
{% ifequal item.jack 'higher' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
{% endifequal %}
{% ifequal item.jack 'unknown' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
{% endifequal %}
{% endifequal %}
{% ifequal name 'du01' %}
{% ifequal item.du01 'lower' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
{% endifequal %}
{% ifequal item.du01 'higher' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
{% endifequal %}
{% ifequal item.du01 'unknown' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
{% endifequal %}
{% endifequal %}
I want to show button depend on different name
.
The name
is a variable now.
However, it map to column name in the table. Is it possible to do something like blow for clear my code?
{% ifequal item.{{name}} 'lower' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="lower">風險比較低</button>
{% endifequal %}
{% ifequal item.{{name}} 'higher' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="higher">風險比較高</button>
{% endifequal %}
{% ifequal item.{{name}} 'unknown' %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary active" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
{% else %}
<button type="button" class="btn btn-block btn-lg btn-outline-primary" style="height: 120px; margin-bottom:100px; margin-height:120;" name="label" value="unknown">無法判斷</button>
{% endifequal %}
Upvotes: 0
Views: 134
Reputation: 1394
You can't do this. What you can do is change the item object to a dictionary instead.
Then you can look up the dictionary value in the template
Upvotes: 1