jack
jack

Reputation: 121

How can I pass jinja variable to the template file that I have created in Flask?

I have a jinja variable and I have passed that variable to the HTML file, but in that HTML, that code has to be passed to another HTML file that is included in it.

For eg:

The current situation is like this:

{% include "navbar.html" %}
<div>
.......
</div>

So, in the HTML file, I want to pass that variable to "navbar.html" as I have to use that variable in the "navbar.html" file.

It would be great if anybody could help me out.

Upvotes: 2

Views: 2551

Answers (2)

Fergus Fitzpatrick
Fergus Fitzpatrick

Reputation: 48

You need to provide more information on how you are passing your variable into your first HTML file and what they look like, in terms of code. If you need to pass a variable through a HTML file (e.g. index.html) that extends another HTML file (nav.html) you could just use the Jinja format:

nav.html:

    {{ variable1 }}

index.html:

     {% extends "nav.html" %}

app.py:

   return render_template("index.html", variable1=variable1)

Hope this helps.

Upvotes: 2

Atte
Atte

Reputation: 308

You shouldn't have to do anything, navbar.html should have access to all the variables in the file that includes it.

I tried a minimal example, where I pass index.html a variable called name and index.html consists of

{% include "base.html" %}

and in base.html, I have:

{{name}}

Which works.

Upvotes: 2

Related Questions