开发者

Django: template inheritance override on include

开发者 https://www.devze.com 2023-01-31 02:41 出处:网络
For example I have top.html which will be incl开发者_如何学Cuded from within base.html. How to override contents of top.html after including?You didn\'t understand templates idealogy.

For example I have top.html which will be incl开发者_如何学Cuded from within base.html. How to override contents of top.html after including?


You didn't understand templates idealogy.
Right way is create base.html like this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Some app - {% block header %}{% endblock %}</title>
    </head>
    <body>
        <h1>{% block title %}Default title{% endblock %}</h1>
        {% block content %}{% endblock %}
    </body>
</html>

And then in page.html you can override header for example:

{% extends "base.html" %}
{% block title %}Some page{% endblock %}
{% block header %}Overriden header{% endblock %}
{% block content %}
    Some content
{% endblock %}
0

精彩评论

暂无评论...
验证码 换一张
取 消