Why we need base for templates?

By using template, we can make pages with same nature bind to one single file to define it.
Maxinumize the flexibilty of whole system.

But sometime we may want to merge pages with same feature.
And the only way to reach this purpose is to extend another page.


Make your first base.html

Base is also a template file.
In YOUR application folder's 'templates' folder:

~$ cd Opensource/opensource_XX/templates
~/Opensource/opensource_XX/templates$ vim base.html

This gives you another new html file called base.html

And put follow code into it.
If you wish to add any code you want, just do so:

<html>
<head>
  <title>{% block page_title %}YunNet OSC{% endblock %}</title>
  {% block head_style %}{% endblock%}
  {% block head_js %}{% endblock %}
</head>
<body>
  {% block body_top_js %}{% endblock %}
  {% block content %}{% endblock %}
  {% block body_bottom_js %}{% endblock %}
</body>
</html>

Structure of this base html allowed pages who extends it can:
(1) Have its own title, and default is 'YunNet OSC'
(2) Ability to edit meta by different page/
(3) Have its own CSS style or JaveScript.
(4) Also provide a body top/bottom section to put javascript whom need to be load in body.

Use it!

Extends a base is very easy.
Just add one single line on top.

{% extends 'base.html' %}

So, just simply open another html file called 'new_index.html'

{% extends 'base.html' %}
{% block page_title %}YunNet OSC - New Index{% endblock %}
{% block content %}
  <h1>This is extends base.html test.</h1>
{% endblock %}

Of course, remeber to add a new define in views.py and routing in urls.py to make sure this page is correctly loaded.

results matching ""

    No results matching ""