askama icon indicating copy to clipboard operation
askama copied to clipboard

[QUESTION] Confusion about template inheritence

Open NilsIrl opened this issue 3 years ago • 6 comments

In the book, the following base template is used:

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>{% block title %}{{ title }} - My Site{% endblock %}</title>
    {% block head %}{% endblock %}
  </head>
  <body>
    <div id="content">
      {% block content %}{% endblock %}
    </div>
  </body>
</html>

What does it mean to have the {{ title }} tag inside of the block? When I override the title using

{% block title %}Homepage{% endblock %}

The entirety of the block is replaced and the following is the result:

<title>Homepage</title>

so I can't see what the {{ title }} changes.

NilsIrl avatar Dec 16 '21 20:12 NilsIrl

You don't have to override the title block. Then "{{ title }} - My Site" is used as a fallback / default.

Kijewski avatar Dec 16 '21 22:12 Kijewski

What is {{ title }} replaced with in that case?

NilsIrl avatar Dec 17 '21 00:12 NilsIrl

The child template overrides:

{% block title %}{{ title }} - My Site{% endblock %}

With:

{% block title %}Homepage{% endblock %}

As your new title block, doesn't contain {{ title }}, then there's nothing to be replaced

vallentin avatar Dec 17 '21 11:12 vallentin

Ohhhh so to be clear here, if the child template doesn't override title, then title must be defined in the Template struct.

Otherwise, it is replaced what it is overriding it with?

NilsIrl avatar Dec 17 '21 20:12 NilsIrl

That's right.

djc avatar Dec 17 '21 20:12 djc

If you can suggest some text for the book that would have clarified this for you, that would be great. Bonus points if you do it as a PR!

djc avatar Dec 17 '21 20:12 djc