Question

Consider this list:

options = [
    {
        'name': 'Option 1',
        'plan': 0b001,
    },
    {
        'name': 'Option 2',
        'plan': 0b010,
    },
    {
        'name': 'Option 3',
        'plan': 0b110,
    },
]

Question: How can I display this list as

         plan1 plan2 plan3
Option 1               ✔
Option 2         ✔      
Option 3   ✔     ✔      

from a template like

{% for option as options %}
<div>
  <div>{{ option.name }}</div>
  <div>{{ option.plan|bitmaskcheck:0b001 }}</div>
  <div>{{ option.plan|bitmaskcheck:0b010 }}</div>
  <div>{{ option.plan|bitmaskcheck:0b100 }}</div>
</div>
{% endfor %}

with a kind of bitmaskcheck operator ? (Or would there be simpler ?)

Was it helpful?

Solution

As suggested by @MikkoOhtamaa, I'll use a dictionnary

options = [
    {
        'name': 'Option 1',
        'plan': {1: True,},
    },
    {
        'name': 'Option 2',
        'plan': {2: True,},
    },
    {
        'name': 'Option 3',
        'plan': {2: True, 3: True,},
    },
]

Then I should be able to make my checks as:

{% for option as options %}
<div>
  <div>{{ option.name }}</div>
  <div>{% if option.plan.1 %}✔{% endif %}</div>
  <div>{% if option.plan.2 %}✔{% endif %}</div>
  <div>{% if option.plan.3 %}✔{% endif %}</div>
</div>
{% endfor %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top