|
| 1 | +# [Tera](https://github.com/Keats/tera) backend extension for `mdbook`'s HTML backend |
| 2 | + |
| 3 | +[](https://crates.io/crates/mdbook-tera-backend) |
| 4 | +[](https://github.com/google/mdbook-i18n-helpers/actions/workflows/test.yml?query=branch%3Amain) |
| 5 | +[](https://github.com/google/mdbook-i18n-helpers/graphs/contributors) |
| 6 | +[](https://github.com/google/mdbook-i18n-helpers/stargazers) |
| 7 | + |
| 8 | +This `mdbook` backend makes it possible to use [tera](https://github.com/Keats/tera) |
| 9 | +templates and expand the capabilities of your books. It works on top of the default HTML |
| 10 | +backend. |
| 11 | + |
| 12 | +## Installation |
| 13 | + |
| 14 | +Run |
| 15 | + |
| 16 | +```shell |
| 17 | +$ cargo install mdbook-tera-backend |
| 18 | +``` |
| 19 | + |
| 20 | +## Usage |
| 21 | + |
| 22 | +### Configuring the backend |
| 23 | + |
| 24 | +To enable the backend, simply add `[output.tera-backend]` to your `book.toml`, |
| 25 | +and configure the place where youre templates will live. |
| 26 | +For instance `theme/templates`: |
| 27 | + |
| 28 | +```toml |
| 29 | +... |
| 30 | + |
| 31 | +[output.tera-backend] |
| 32 | +templates_dir = "theme/templates" |
| 33 | + |
| 34 | +... |
| 35 | +``` |
| 36 | + |
| 37 | +### Creating templates |
| 38 | + |
| 39 | +Create your template files in the same directory as your book. |
| 40 | + |
| 41 | +```html |
| 42 | +<!-- ./theme/templates/language_list.html --> |
| 43 | +<ul> |
| 44 | +{% for identifier, language_name in get_context(key="output.i18n.languages") %} |
| 45 | + <li>{{ identifier }}: {{ language_name }}</li> |
| 46 | +{% endfor %} |
| 47 | +</ul> |
| 48 | +``` |
| 49 | + |
| 50 | +### Using templates in `index.hbs` |
| 51 | + |
| 52 | +Since the HTML renderer will first render Handlebars templates, we need to tell it to |
| 53 | +ignore tera templates using `{{{{raw}}}}` blocks: |
| 54 | + |
| 55 | +```html |
| 56 | +{{{{raw}}}} |
| 57 | +{% set current_language = ctx | get(key="config") | get(key="book") | get(key="language", default="en") %} |
| 58 | +<p>CURRENT LANGUAGE: {{ current_language }}</p> |
| 59 | +<p>All languages: {% include "language_list.html" %}</p> |
| 60 | +{{{{/raw}}}} |
| 61 | +``` |
| 62 | + |
| 63 | +Includes names are based on the file name and not the whole file path. |
| 64 | + |
| 65 | +### Tera documentation |
| 66 | + |
| 67 | +Find out all you can do with Tera templates [here](https://keats.github.io/tera/docs/). |
| 68 | + |
| 69 | +## Changelog |
| 70 | + |
| 71 | +Please see [CHANGELOG](../CHANGELOG.md) for details on the changes in each |
| 72 | +release. |
| 73 | + |
| 74 | +## Contact |
| 75 | + |
| 76 | +For questions or comments, please contact |
| 77 | +[Martin Geisler ](mailto:[email protected]) or |
| 78 | +[Alexandre Senges ](mailto:[email protected]) or start a |
| 79 | +[discussion](https://github.com/google/mdbook-i18n-helpers/discussions). We |
| 80 | +would love to hear from you. |
| 81 | + |
| 82 | +--- |
| 83 | + |
| 84 | +This is not an officially supported Google product. |
0 commit comments