2

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.

2
  • Does this answer your question? Python Flask: pass Jinja variable to backend Commented Jun 4, 2020 at 15:08
  • The thing that are you trying to say is different. I was facing the problem that is answered below and it is working fine now. Commented Jun 4, 2020 at 16:25

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, thank you! I tried it and it worked. We don't need to do anything, it takes the parameters itself.
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.