Home > Back-end >  jinja2 : how to match string to json field
jinja2 : how to match string to json field

Time:10-27

I'm having a problem in a jinja template. If I have an object 'jsonobj' like so:

{
    "type": "test",
    "people": [{"name": "bill", "age": 43}]
}

in my template I'm trying to do an if like

  {% if 'test' is jsonobj.type %}
  <!-- some html here -->
  {% else %}
  <!-- some other html here -->
  {% endif %}

but I get an error: jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'endfor'. You probably made a nesting mistake. Jinja is expecting this tag, but currently looking for 'elif' or 'else' or 'endif'. The innermost block that needs to be closed is 'if'.

when I add curlies around jsonobj.type I get

jinja2.exceptions.TemplateSyntaxError: expected token 'name', got '{'

Was looking at the docs here and I thought it should work...any ideas? thanks

I tried any variation I could think of; ==, is, isin and so on. Tried wrapping json obj in one and two sets of curlies. Did lots of googling

CodePudding user response:

May be you forget endif tag at end?

    {% if 'test' is jsonobj.type %}
    ...
    {% else %}
    ...
    {% endif %}

CodePudding user response:

{% endif %} it seems you forgot to turn off the if blog

  • Related