'Is there any reason to use {% block name %} over {{ self.name() }} in Jinja templating?

I ran into the problem of wanting to reuse the same block of content multiple times in the same base template. This lead me to use the following:

<li>
  {% block backup %} {% endblock %}
</li>
...
<li>
  {{ self.backup() }}
</li>

Is there any advantage to using block backup one time and self.backup() for every subsequent time compared to just using self.backup() every time? From my very basic testing there is no direct difference on the website. It runs and looks the same with both variants.

The website is just a small internal FAQ at my company so performance is not really a concern.



Solution 1:[1]

One use-case where {% block name %}{% endblock %} works but {{ self.name()}} does not is when templating optional content. Anything designated with self has to exist in the page that extends it.
Example:

base.html

<head>
  ...
  <link rel="stylesheet" type="text/css" href="static/css/reset.css"/>
  <link rel="stylesheet" type="text/css" href="static/css/styles.css"/>
  
  {{ self.stylesheet() }}

</head>

If a page extending base.html does not contain {% block stylesheet %}...{% endblock %} the page will fail to load with a 500 server error when it cannot find the stylesheet. This does not happen if it is declared as {% block stylesheet %}...{% endblock %} in base.html, it just gets skipped.
(The same behaviour can be used with {% block name %} by adding the required keyword.)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1