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:

    1. "variables": {
    2. "myVariable": "Hello World"
    3. }
    4. }

    Display Variables

    Variables specified in the book.json are accessible under the scope book:

    1. {{ book.foo.bar }}
    2. {{ 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:

    1. {% if hungry %}
    2. I am hungry
    3. I am tired
    4. I am good!
    5. {% endif %}

    for

    for iterates over arrays and dictionaries.

    Let’s consider your variables in the book.json:

    1. # Authors
    2. {% for author in book.authors %}
    3. {% endfor %}

    The above example lists all the authors using the name attribute of each item in the array as the display value.

    include