/
heading.txt
82 lines (56 loc) · 2.04 KB
/
heading.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#######
Heading
#######
The "heading" template tag library provides 1 template tag:
* headingcontext: helps you manage heading levels in HTML code. Requires a
{% endheadingcontext %} closing tag.
**************
headingcontext
**************
With cascading templates, includes and bases, some parts of template code
could be reused in different heading contexts.
As an example, consider the following "home page" code in home.html:
.. code-block:: html
<h1>My beautiful website</h1>
<h2>News</h2>
<p>... links to news ...</p>
And consider the following "news page" code in news.html
.. code-block:: html
<h1>News</h1>
<p>... links to news ...</p>
You cannot reuse (include) news.html code into home.html, because the heading
level does not match.
The "headingcontext" template tag allows you to solve this problem.
Here is modified home.html:
.. code-block:: django
{% load heading %}
<h1>My beautiful website</h1>
{% headingcontext %}
{% include "news.html" %}
{% endheadingcontext %}
Ok. Now, what if the news.html code was using h5 in place of h1 like that:
.. code-block:: html
<h5>News</h5>
<p>... links to news ...</p>
You can use the additional "source_level" parameter in home.html:
.. code-block:: django
{% load heading %}
<h1>My beautiful website</h1>
{% headingcontext source_level=5 %}
{% include "news.html" %}
{% endheadingcontext %}
This causes all heading of level 5 and greater in news.html to be relative
to the current heading level (2).
You can use nested {% headingcontext %}{% endheadingcontext %} calls. As an
example, news.html could be:
.. code-block:: django
<h5>News</h5>
<p>... links to news ...</p>
{% load heading %}
{% headingcontext source_level=3 target_level=6 %}
{% include "another_template_fragment_which_contains_some_h3.html" %}
{% endheadingcontext %}
Notice the use of the additional "target_level" parameter, which forces
ouput levels to start at 6.
You can read the provided test cases to observe what does this template tag at
tests.HeadingContextTemplateTagTestCase.