Search code examples
pythonflaskjinja2

Getting jinja2.exceptions.TemplateSyntaxError


I'm a total noob to flask. I'm just getting started with flask and jinja template engine. Please, kindly answer me. This is my html head tag,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% if {{content}} %}
    <title>WAJ - {{content}}</title>
    {% else %}
        <title>WAJ</title>
    {% endif %}
</head>

The content variable is a string passed from flask, But I'm getting this error

 File "d:\python programs\Flask Project\templates\user.html", line 6, in template
    {% if {{content}} %}
jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

I tried re-reading the code and checked for what could have gone wrong. But didn't find a solution. please help. 🙏


Solution

  • Well the issue in your code is simple. You don't need double curly braces {{ }} inside the {% if %} block.

        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            {% if content %}
                <title>WAJ - {{ content }}</title>
            {% else %}
                <title>WAJ</title>
            {% endif %}
        </head>
    

    Just replace the {{content}} with content and you are good to go. I would like to give you a nice little suggestion since you are a newbie, that is... Whenever you get a syntax error or something like that, you should look up to the documentation. It is a good habit that every programmer should adopt. Links to the documentations:

    https://flask.palletsprojects.com/en/2.3.x/templating/

    https://jinja.palletsprojects.com/en/3.1.x/