Reputation: 162
Based on the user's permissions, I want to pass in a variable to an admin template to include in a Jinja template. If the variable isn't passed in, the user didn't have permission and I don't want to include anything.
@app.route("/")
def index():
if user_status == "admin":
return render_template("index.html", admin_management="admin.html")
return render_template("index.html", userName=userName)
</li>
{% include admin_management ignore missing %}
</ul>
</div>
I thought ignore missing
would tell Jinja to ignore the variable if it wasn't passed, but I get an UndefinedError
.
jinja2.exceptions.UndefinedError: 'admin_management' is undefined
How do I include a template only if the variable naming it is passed in?
Upvotes: 2
Views: 1807
Reputation: 127180
ignore missing
refers to ignoring if the template with the given name is missing, not if the variable that contains the name is missing. Use an if
block if the variable might not be defined.
{% if admin_management is defined %}
{% include admin_management %}
{% endif %}
You probably don't need ignore missing
in your case, but you can leave it in if the template might be missing too.
Upvotes: 1