Jinja2 template not rendering if-elif-else statement properly Jinja2 template not rendering if-elif-else statement properly python python

Jinja2 template not rendering if-elif-else statement properly


You are testing if the values of the variables error and Already are present in RepoOutput[RepoName.index(repo)]. If these variables don't exist then an undefined object is used.

Both of your if and elif tests therefore are false; there is no undefined object in the value of RepoOutput[RepoName.index(repo)].

I think you wanted to test if certain strings are in the value instead:

{% if "error" in RepoOutput[RepoName.index(repo)] %}    <td id="error"> {{ RepoOutput[RepoName.index(repo)] }} </td>{% elif "Already" in RepoOutput[RepoName.index(repo) %}    <td id="good"> {{ RepoOutput[RepoName.index(repo)] }} </td>{% else %}    <td id="error"> {{ RepoOutput[RepoName.index(repo)] }} </td>{% endif %}</tr>

Other corrections I made:

  • Used {% elif ... %} instead of {$ elif ... %}.
  • moved the </tr> tag out of the if conditional structure, it needs to be there always.
  • put quotes around the id attribute

Note that most likely you want to use a class attribute instead here, not an id, the latter must have a value that must be unique across your HTML document.

Personally, I'd set the class value here and reduce the duplication a little:

{% if "Already" in RepoOutput[RepoName.index(repo)] %}    {% set row_class = "good" %}{% else %}    {% set row_class = "error" %}{% endif %}<td class="{{ row_class }}"> {{ RepoOutput[RepoName.index(repo)] }} </td>