This is my base.html:
<html>
<head>
{% block head %}
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
This is my meta.html:
{% extends 'templates/base.html' %}
{% block head %}
{% if page == 'index' %}
<meta name="mysite.com" content="{{page}}"></meta>
{% else %}
<meta name="mysite.com" content="other page"></meta>
{% endblock %}
This is my index.html:
{% extends 'templates/base.html' %}
{% block body %}
......
{% endblock %}
This is my view:
@view_config(route_name='index', renderer='templates/index.html', permission='view')
def index(request):
return dict(page="index")
Extending the body works, but the meta tags are not showing up at all. What am I doing wrong?
As per Jinja2 documentation on extends
, "The extends tag can be used to extend a template from another one. You can have multiple of them in a file but only one of them may be executed at the time."
When you load index.html
from your view, the templating engine assesses index.html
. It sees {% extends 'templates/base.html' %}
in index.html
and uses the blocks within index.html
to replace same-name blocks of the parent/extended template (base.html
).
So index.html
says to use base.html
as the skeleton template, but to replace {% block body %}
in the parent (base.html
) with its own {% block body %}
.
Nowhere in this instruction (neither in base.html
, nor index.html
, nor the view code) does meta.html
get mentioned. The templating engine isn't analyzing all the templates in your directory structure to see how it can plug things in. It is only using your explicit directives.
To be explicit about using meta.html
, you can use the include
directive (documentation found here) in base.html
. Your base.html
would then look like this:
<html>
<head>
{% include 'templates/meta.html' %}
{% block head %}
{% endblock %}
</head>
<body>
{% block body %}
{% endblock %}
</body>
</html>
This would pull in meta.html
and replace the same-name blocks of the skeleton (base.html
) with the ones that were retrieved from the included template (meta.html
).
In addition, the extends
statement within meta.html
needs to be removed, so it doesn't create a circular template inclusion.