diff options
Diffstat (limited to 'docs/templating')
-rw-r--r-- | docs/templating/README.md | 90 | ||||
-rw-r--r-- | docs/templating/conrefs.md | 55 | ||||
-rw-r--r-- | docs/templating/variables.md | 64 |
3 files changed, 209 insertions, 0 deletions
diff --git a/docs/templating/README.md b/docs/templating/README.md new file mode 100644 index 0000000..38df38e --- /dev/null +++ b/docs/templating/README.md @@ -0,0 +1,90 @@ +# Templating + +GitBook uses the Nunjucks templating language to process pages and theme's templates. + +The Nunjucks syntax is very similar to **Jinja2** or **Liquid**. + +### Variables + +A variable looks up a value from the template context. If you wanted to simply display a variable, you would do: + +```twig +{{ username }} +``` + +This looks up username from the context and displays it. Variable names can have dots in them which lookup properties, just like javascript. You can also use the square bracket syntax. + +```twig +{{ foo.bar }} +{{ foo["bar"] }} +``` + +If a value is undefined, nothing is displayed. The following all output nothing if foo is undefined: `{{ foo }}`, `{{ foo.bar }}`, `{{ foo.bar.baz }}`. + +GitBook provides a set of [context variables](variables.md). + +### Filters + +Filters are essentially functions that can be applied to variables. They are called with a pipe operator (`|`) and can take arguments. + +```twig +{{ foo | title }} +{{ foo | join(",") }} +{{ foo | replace("foo", "bar") | capitalize }} +``` + +The third example shows how you can chain filters. It would display "Bar", by first replacing "foo" with "bar" and then capitalizing it. + +### Tags + +##### if + +`if` tests a condition and lets you selectively display content. It behaves exactly as javascript's if behaves. + +```twig +{% if variable %} + It is true +{% endif %} +``` + +If variable is defined and evaluates to true, "It is true" will be displayed. Otherwise, nothing will be. + +You can specify alternate conditions with elif and else: + +```twig +{% if hungry %} + I am hungry +{% elif tired %} + I am tired +{% else %} + I am good! +{% endif %} +``` + +##### for + +`for` iterates over arrays and dictionaries. + +```twig +# Chapters about GitBook + +{% for article in glossary.terms['gitbook'].articles %} +* [{{ article.title }}]({{ article.path }}) +{% endfor %} +``` + +##### set + +`set` lets you create/modify a variable. + +```twig +{% set softwareVersion = "1.0.0" %} + +Current version is {{ softwareVersion }}. +[Download it](website.com/download/{{ softwareVersion }}) +``` + +##### include and block + +Inclusion and inheritance is detailled in the [ConRefs](conrefs.md) section. + diff --git a/docs/templating/conrefs.md b/docs/templating/conrefs.md new file mode 100644 index 0000000..7844e9c --- /dev/null +++ b/docs/templating/conrefs.md @@ -0,0 +1,55 @@ +# Content References + +Content referencing (conref) is a convenient mechanism for reuse of content from other files or books. + +### Importing local files + +Importing an other file's content is really easy using the `include` tag: + +``` +{% include "./test.md" %} +``` + +### Importing file from another book + +GitBook can also resolve the include path by using git: + +``` +{% include "git+https://github.com/GitbookIO/documentation.git/README.md#0.0.1" %} +``` + +The format of git url is: + +``` +git+https://user@hostname/project/blah.git/file#commit-ish +``` + +The real git url part should finish with `.git`, the filename to import is extracted after the `.git` till the fragment of the url. + +The `commit-ish` can be any tag, sha, or branch which can be supplied as an argument to `git checkout`. The default is `master`. + +### Inheritance + +Template inheritance is a way to make it easy to reuse templates. When writing a template, you can define "blocks" that child templates can override. The inheritance chain can be as long as you like. + +`block` defines a section on the template and identifies it with a name. Base templates can specify blocks and child templates can override them with new content. + +``` +{% extends "./mypage.md" %} + +{% block pageContent %} +# This is my page content +{% endblock %} +``` + +In the file `mypage.md`, you should specify the blocks that can be extent: + +``` +{% block pageContent %} +This is the default content +{% endblock %} + +# License + +{% import "./LICENSE" %} +``` diff --git a/docs/templating/variables.md b/docs/templating/variables.md new file mode 100644 index 0000000..3dbd6f7 --- /dev/null +++ b/docs/templating/variables.md @@ -0,0 +1,64 @@ +# Variables + +The following is a reference of the available data during book's parsing and theme generation. + +### Global Variables + +| Variable | Description | +| -------- | ----------- | +| `book` | Bookwide information + configuration settings from `book.json`. See below for details. | +| `gitbook` | GitBook specific information | +| `page` | Current page specific information | +| `file` | File associated with the current page specific information | +| `summary` | Information about the table of contents | +| `languages` | List of languages for multi-lingual books | +| `config` | Dump of the `book.json` | + +### Book Variables + +| Variable | Description | +| -------- | ----------- | +| `book.[CONFIGURATION_DATA]` | All the `variables` set via the `book.json` are available through the book variable. | +| `book.language` | Current language for a multilingual book | + +### GitBook Variables + +| Variable | Description | +| -------- | ----------- | +| `gitbook.time` | The current time (when you run the `gitbook` command). | +| `gitbook.version` | Version of GitBook used to generate the book | + +### File Variables + +| Variable | Description | +| -------- | ----------- | +| `file.path` | The path to the raw page | +| `file.mtime` | Modified Time, Time when file data last modified | +| `file.type` | The name of the parser used to compile this file (ex: `markdown`, `asciidoc`, etc) | + +#### Page Variables + +| Variable | Description | +| -------- | ----------- | +| `page.title` | Title of the page | +| `page.previous` | Previous page in the Table of Contents (can be `null`) | +| `page.next` | Next page in the Table of Contents (can be `null`) | +| `page.dir` | Text direction, based on configuration or detected from content (`rtl` or `ltr`) | + +#### Table of Contents Variables + +| Variable | Description | +| -------- | ----------- | +| `summary.parts` | List of sections in the Table of Contents | + +Thw whole table of contents (`SUMMARY.md`) can be accessed: + +`summary.parts[0].articles[0].title` will return the title of the first article. + +#### Multi-lingual book Variable + +| Variable | Description | +| -------- | ----------- | +| `languages.list` | List of languages for this book | + +Languages are defined by `{ id: 'en', title: 'English' }`. |