Debian Codegraph @

guide/src/format/theme/index-hbs.md

raw
1 # index.hbs
2
3 `index.hbs` is the handlebars template that is used to render the book. The
4 markdown files are processed to html and then injected in that template.
5
6 If you want to change the layout or style of your book, chances are that you
7 will have to modify this template a little bit. Here is what you need to know.
8
9 ## Data
10
11 A lot of data is exposed to the handlebars template with the "context". In the
12 handlebars template you can access this information by using
13
14 ```handlebars
15 {{name_of_property}}
16 ```
17
18 Here is a list of the properties that are exposed:
19
20 - ***language*** Language of the book in the form `en`, as specified in `book.toml` (if not specified, defaults to `en`). To use in <code
21 class="language-html">\<html lang=\"{{ language }}\"></code> for example.
22 - ***title*** Title used for the current page. This is identical to `{{ chapter_title }} - {{ book_title }}` unless `book_title` is not set in which case it just defaults to the `chapter_title`.
23 - ***book_title*** Title of the book, as specified in `book.toml`
24 - ***chapter_title*** Title of the current chapter, as listed in `SUMMARY.md`
25
26 - ***path*** Relative path to the original markdown file from the source
27 directory
28 - ***content*** This is the rendered markdown.
29 - ***path_to_root*** This is a path containing exclusively `../`'s that points
30 to the root of the book from the current file. Since the original directory
31 structure is maintained, it is useful to prepend relative links with this
32 `path_to_root`.
33 - ***previous*** and ***next*** These are objects used for linking to the previous and next chapter. They contain the properties `title` and `link` of the corresponding chapter.
34 - ***chapters*** Is an array of dictionaries of the form
35 ```json
36 {"section": "1.2.1", "name": "name of this chapter", "path": "dir/markdown.md"}
37 ```
38 containing all the chapters of the book. It is used for example to construct
39 the table of contents (sidebar).
40
41 ## Handlebars helpers
42
43 In addition to the properties you can access, there are some handlebars helpers
44 at your disposal.
45
46 ### toc
47
48 The toc helper is used like this
49
50 ```handlebars
51 {{#toc}}{{/toc}}
52 ```
53
54 and outputs something that looks like this, depending on the structure of your
55 book
56
57 ```html
58 <ul class="chapter">
59 <li><a href="link/to/file.html">Some chapter</a></li>
60 <li>
61 <ul class="section">
62 <li><a href="link/to/other_file.html">Some other Chapter</a></li>
63 </ul>
64 </li>
65 </ul>
66 ```
67
68 If you would like to make a toc with another structure, you have access to the
69 chapters property containing all the data. The only limitation at the moment
70 is that you would have to do it with JavaScript instead of with a handlebars
71 helper.
72
73 ```html
74 <script>
75 var chapters = {{chapters}};
76 // Processing here
77 </script>
78 ```
79
80 ### resource
81
82 The path to a static file.
83 It implicitly includes `path_to_root`,
84 and accounts for files that are renamed with a hash in their filename.
85
86 ```handlebars
87 <link rel="stylesheet" href="{{ resource "css/chrome.css" }}">
88 ```
89
90 ### fa
91
92 mdBook includes a copy of [Font Awesome Free's](https://fontawesome.com)
93 MIT-licensed SVG files. It accepts three positional arguments:
94
95 1. Type: one of "solid", "regular", and "brands" (light and duotone are not
96 currently supported)
97 2. Icon: anything chosen from the
98 [free icon set](https://fontawesome.com/v6/search)
99 3. ID (optional): if included, an HTML ID attribute will be added to the
100 icon's wrapping `<span>` tag
101
102 For example, this handlebars syntax will become this HTML:
103
104 ```handlebars
105 {{fa "solid" "print" "print-button"}}
106 ```
107
108 ```html
109 <span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M448 192V77.25c0-8.49-3.37-16.62-9.37-22.63L393.37 9.37c-6-6-14.14-9.37-22.63-9.37H96C78.33 0 64 14.33 64 32v160c-35.35 0-64 28.65-64 64v112c0 8.84 7.16 16 16 16h48v96c0 17.67 14.33 32 32 32h320c17.67 0 32-14.33 32-32v-96h48c8.84 0 16-7.16 16-16V256c0-35.35-28.65-64-64-64zm-64 256H128v-96h256v96zm0-224H128V64h192v48c0 8.84 7.16 16 16 16h48v96zm48 72c-13.25 0-24-10.75-24-24 0-13.26 10.75-24 24-24s24 10.74 24 24c0 13.25-10.75 24-24 24z"/></svg></span>
110 ```