django templates - Jinja 2 : escaping and extending -


i trying output html after running templates through jinja2 , thereafter through django. {{ 'raw' }} , {% raw %}...{% endraw %} tags not doing expect , there not lot of documentation on nor googling yield helpful.

as example consider following series of templates. first stock jinja2 base page 1 wants extend.

{# jinja_base.html #} {% block jinja_head %}  jinja head {% endblock jinja_head %} {% block jinja_body %}  jinja body {% endblock jinja_body %} {% block jinja_foot %}  jinja foot {% endblock jinja_foot %} 

the second stock django template page 1 wants extend.

{# django_base.html #} {% block django_head %}  django head {% endblock django_head %} {% block django_body %}  django body {% endblock django_body %} {% block django_foot %}  django foot {% endblock django_foot %} 

to extend both of them have jinja/django mixin template reads follows. idea run through jinja first through django.

{# mixin.html #} {{ '{% extends "django_base.html" %}' }} {% extends "jinja_base.html" %} {{ '{% block django_head %}' }} {% block jinja_head %}  mixin head {% endblock jinja_head %} {{ '{% endblock django_head %}' }} {{ '{% block django_body %}' }} {% block jinja_body %}  mixin body {% endblock jinja_body %} {{ '{% endblock django_body %}' }} {{ '{% block django_foot %}' }} {% block jinja_foot %}  jinja foot {% endblock jinja_foot %} {{ '{% endblock django_foot %}' }} 

after jinja run, i'm expecting following output

{% extends "django_base.html" %} {% block django_head %}  mixin head {% endblock django_head %} {% block django_body %}  mixin body {% endblock django_body %} {% block django_foot %}  jinja foot {% endblock django_foot %} 

however, getting following instead.

{% extends "jinja_base.html" %}  mixin head  mixin body  jinja foot 

that django code embedded in jinja escape sequences getting stripped. first tag retained.

the docs mention before extends kept after not. there no explanation on how best circumvent nor why affects raw/escaped code.

hmm.. perhaps must place extends right @ end ?

after extend template have put content between blocks. every other stuff ignored. in mixin.html:

{{ '{% block django_head %}' }}   <--- ignored {% block jinja_head %}  mixin head {% endblock jinja_head %} {{ '{% endblock django_head %}' }} <--- ignored 

the first , last line outside of jinja_head block, ignored.

the correct way straightforward, put django lines between jinja's blocks, e.g.:

{% block jinja_head %} {{ '{% block django_head %}' }} mixin head {{ '{% endblock django_head %}' }} {% endblock jinja_head %} 

this results:

{% block django_head %}  mixin head  {% endblock django_head %} 

for {{ '{% extends "django_base.html" %}' }} define empty block in jinja_base.html, can override in mixin.html django extends line. complete example:

jinja_base.html

{# jinja_base.html #}  {% block django_extends %} {% endblock django_extends %}  {% block jinja_head %} jinja head {% endblock jinja_head %}  {% block jinja_body %} jinja body {% endblock jinja_body %}  {% block jinja_foot %} jinja foot {% endblock jinja_foot %} 

mixin.html

{# mixin.html #} {% extends "jinja_base.html" %}  {% block django_extends %} {{ '{% extends "django_base.html" %}' }} {% endblock django_extends %}  {% block jinja_head %} {{ '{% block django_head %}' }} mixin head {{ '{% endblock django_head %}' }} {% endblock jinja_head %}  {% block jinja_body %} {{ '{% block django_body %}' }}  mixin body {{ '{% endblock django_body %}' }} {% endblock jinja_body %}  {% block jinja_foot %} {{ '{% block django_foot %}' }}  jinja foot {{ '{% endblock django_foot %}' }} {% endblock jinja_foot %} 

this way see expected result after jinja's render:

{% extends "django_base.html" %}  {% block django_head %}  mixin head {% endblock django_head %}  {% block django_body %}  mixin body {% endblock django_body %}  {% block django_foot %}  jinja foot {% endblock django_foot %} 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -