Jinja: How does the helper null master example work?

Sample code from the official jinja website:

{% if not standalone %}{% extends 'master.html' %}{% endif -%} <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <title>{% block title %}The Page Title{% endblock %}</title> <link rel="stylesheet" href="style.css" type="text/css"> {% block body %} <p>This is the page body.</p> {% endblock %} 

As I understand it, when autonomously true, the following code is output:

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <title>{% block title %}The Page Title{% endblock %}</title> <link rel="stylesheet" href="style.css" type="text/css"> {% block body %} <p>This is the page body.</p> {% endblock %} 

And if the offline value is false, this prints:

 {% if not standalone %} <<master.html code>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <title>{% block title %}The Page Title{% endblock %}</title> <link rel="stylesheet" href="style.css" type="text/css"> {% block body %} <p>This is the page body.</p> {% endblock %} 

It seems very strange. I am clearly missing something obvious, what is it?

+4
source share
1 answer

What is not immediately visible from the documentation is that when a template extends another template, only blocks in the child template that have an instance of the parent template are displayed. Everything else is discarded.

So, in non-standard mode:

 {% if not standalone %} {% only care about blocks also in "master.html" %} {% endif %} {# Everything below is ignored #} <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <title>{# The following will be rendered if master has a block named title. #} {% block title %}The Page Title{% endblock %} {# All the following will be ignored #}</title> <link rel="stylesheet" href="style.css" type="text/css"> {# This *may* be rendered, if master.html has a block named "body" #} {% block body %} <p>This is the page body.</p> {% endblock %} 
+5
source

Source: https://habr.com/ru/post/1399555/


All Articles