1

I'm not sure if this is a bug or not, but there is something weird going on if I try to compare an integer form a dropdown field to an integer in the template. In short, it doesn't work.

I'm working with a dropdown level field for users from 1 to 15 and use conditionals for content:

{% set integer = 7 %}
{% set mylevel = currentUser.level %}

My level: {{mylevel}}<br>
Number: {{integer}}<br><br>

{% if mylevel >= integer %}
    {{mylevel}} bigger than or equals {{integer}}
{% else %}
    {{mylevel}} smaller than {{integer}}    
{% endif %}

This outputs:

My level: 8<br>
Number: 7<br><br>
8 smaller than 7

HOWEVER if I replace the code "currentUser.level" with 8 like:

{% set integer = 7 %}
{% set mylevel = 8 %}

My level: {{mylevel}}<br>
Number: {{integer}}<br><br>

{% if mylevel >= integer %}
    {{mylevel}} bigger than or equals {{integer}}
{% else %}
    {{mylevel}} smaller than {{integer}}    
{% endif %}

Then the output is:

My level: 8
Number: 7
8 bigger than or equals 7

I checked the database and reinserted all numbers, but nothing changes.

Database had this:

{"options":[{"label":"SG1","value":"1","default":""},{"label":"SG2","value":"2","default":""},{"label":"SG3","value":"3","default":""},{"label":"SG4","value":"4","default":""},{"label":"SG5","value":"5","default":""},{"label":"SG6","value":"6","default":""},{"label":"SG7","value":"7","default":""},{"label":"SG8","value":"8","default":""},{"label":"SG9","value":"9","default":""},{"label":"SG10","value":"10","default":""},{"label":"SG11","value":"11","default":""},{"label":"SG12","value":"12","default":""},{"label":"TG01","value":"13","default":""},{"label":"TG02","value":"14","default":""},{"label":"TG03","value":"15","default":""},{"label":"TG04","value":"16","default":""}]}
2
  • When you output the {{currentUser.level}} in your templates what does it give you?
    – Jan_dh
    Jan 25 '17 at 17:56
  • This outputs 8 as you can see in the second example block
    – noregt
    Jan 25 '17 at 19:18
1

You'll want to use:

{% set mylevel = currentUser.level.value %}

If you just use currentUser.level and assign it to a variable, then what you'll get back is a MultiOptionsFieldData class, instead of the value you are expecting.

There is a note on the bottom of the page here that describes it.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.