Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jinja: How to override variables in super block?

I've got a template parent.tpl:

{% set myvar = 'AAA' %}

{% block par %}
{{ myvar }}
{% endblock %}

and a child.tpl

{% extends "parent.tpl" %}

{% block par %}
{% set myvar = 'BBB' %}
{{ super() }}
{% endblock %}

the child.tpl results:

AAA

but not

BBB

How can I get BBB output with variable override?

Thanks

like image 370
zhangailin Avatar asked Oct 31 '22 10:10

zhangailin


1 Answers

If you're using Flask, you can use a global variable like g.myvar. It will be available in every template.

Take a look at Pass variables from child template to parent in Jinja2.

like image 172
Igor Hatarist Avatar answered Nov 04 '22 06:11

Igor Hatarist