0

I want to pass a variable inside an include statement:

init.py:

@app.route("/about")
def about():
    return render_template("index.html", content="about.html")

index.html:

{% include {{ content }} %} 

does not work!

Dirkiboy
  • 67
  • 8
  • How do you want your site to look? This seems like backend work to me, not the remit of jinja – roganjosh Jul 30 '20 at 14:07
  • 1
    I feel almost certain that this is an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – roganjosh Jul 30 '20 at 14:09

2 Answers2

1

As this thread pops up via the google search and the answer is pretty straight forward:

Just do not use the {{}} syntax inside the include statement. So instead of

{% include {{ content }} %} 

use

{% include content %} 

which will evaluate the variable set in render_template

Flo
  • 448
  • 2
  • 6
0

This question has already been asked and answered.

How to pass selected, named arguments to Jinja2's include context?

Use utility processor to pass in variables to your template.

With the following code, foo will be defined and available to all of your templates.

@app.context_processor
def utility_processor():
    rval = dict()
    
    rval['foo']   = 'bar'
shrewmouse
  • 5,338
  • 3
  • 38
  • 43
  • What's this got to do with `{% include %}` for template inheritance? – roganjosh Jul 30 '20 at 14:22
  • ‘index.html’ consists of the usual html code **+** an ‘include’ statement which inherits code from about.html. But instead writing ‘{% include “about.html“ %}’, I wanna pass the variable ‘content’ into the statement. – Dirkiboy Jul 30 '20 at 17:50
  • @shrewmouse No, it hasn't been asked. The post you are referring to is about passing a variable thru a template to another template instead of using it directly in the `include` statement. – Dirkiboy Jul 31 '20 at 01:59