Templating
If you want to output any of the special templating tags, you can use raw and anything inside of it will be output as plain text.
A variable looks up a value from the book context.
Variables are defined in the file:
"variables": {
"myVariable": "Hello World"
}
}
Display Variables
Variables specified in the book.json
are accessible under the scope book
:
{{ book.foo.bar }}
{{ book["bar"] }}
If a value is undefined, nothing is displayed. The following all output nothing if is undefined: {{ foo }}
, {{ foo.bar }}
, {{ foo.bar.baz }}
.
Context variables
Some variables are also available to get informations about the current file or the GitBook instance:
Tags are special blocks that perform operations on sections of the template.
If
if tests a condition and lets you selectively display content. It behaves exactly as a programming language’s if behaves.
You can specify alternate conditions with elif and else:
{% if hungry %}
I am hungry
I am tired
I am good!
{% endif %}
for
for iterates over arrays and dictionaries.
Let’s consider your variables in the book.json
:
# Authors
{% for author in book.authors %}
{% endfor %}
The above example lists all the authors using the name
attribute of each item in the array as the display value.