'Show variable from flask in include html page

I have header.html:

<body>
    <a class="navbar-brand mr-4 d-flex align-items-center" href="{{ url_for('dash') }}">
        <img class="img-fluid" src="../static/assets/images/live-steam/logo.png" alt="Image">
        <p class="px-1 font-weight-bold font-14">{{sys_domain}}</p>
    </a>
</body>

and .py code:

@flask.route('/header')
def header():
  cur = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
  cur.execute('SELECT * FROM system_settings')
  user = cur.fetchone()

  sys_domain = (user['system_name'])

  return render_template("header.html", sys_domain=sys_domain)

When i include this header page to another page '{{sys_domain}}' show nothing!

example of page that header.html include to it:

<body>
   <header>
      {% include 'header.html' %}
   </header>
</body>


Solution 1:[1]

I believe it is because when you try to use include, it will not call via the flask route. It is including the template directly and rendering it. You can check this official template documentation link

You can use the "with" keyword of jinja2 to call it that way.

You can check this link to have an idea of this.

You can retrieve the user['system_name'] from mysql as sys_domain variable in the route function of .py code from where you are calling the html file in which header.html is to be called. Then you can do something like this.

{% with sys_domain=sys_domain %}
    {% include 'header.html' %}
{% endwith %}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1