Skip to content
This repository was archived by the owner on Nov 14, 2023. It is now read-only.

Commit ab48306

Browse files
author
Florens Verschelde
committed
Add documentation for includes and namespaces
1 parent 3d36e97 commit ab48306

File tree

1 file changed

+52
-2
lines changed

1 file changed

+52
-2
lines changed

doc/templating.md

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,56 @@ By default, Twig will escape HTML tags and entities (to help prevent [cross-site
7373
```
7474

7575

76+
Including stuff
77+
---------------
78+
79+
A very common pattern when using Twig is to create a “base” template that other templates will extend. For instance:
80+
81+
```twig
82+
{# site/templates/base.twig #}
83+
<!doctype html>
84+
<html lang="{{ site.language() }}">
85+
<head>
86+
<title>{{ htmlTitle|default('No title') }}</title>
87+
</head>
88+
<body>
89+
{% include '@snippets/navigation.twig' %}
90+
{% block content %}
91+
{% endblock %}
92+
</body>
93+
</html>
94+
```
95+
96+
Then, in a specific template, you can extend this base template:
97+
98+
```twig
99+
{# site/templates/default.twig #}
100+
{% extends '@templates/base.twig' %}
101+
{% set htmlTitle = page.title ~ ' - ' ~ site.title %}
102+
103+
{% block content %}
104+
{{ page.text.kirbytext | raw }}
105+
{% endblock %}
106+
```
107+
108+
See the `@snippets` and `@templates` parts in our extends and includes? These are Twig namespaces, i.e. aliases for specific directories. Out of the box we have 4 namespaces:
109+
110+
- `@templates`
111+
- `@snippets`
112+
- `@plugins`
113+
- `@assets`
114+
115+
They all point to the corresponding directory (generally `site/snippets`, `site/plugins` etc.), and follow Kirby’s configuration if you have changed those paths.
116+
117+
Finally, note that you can use the `source()` function to output the contents of a file that is not a template, for instance if you want to inline a short script in a page:
118+
119+
```twig
120+
<script>
121+
{{ source('@assets/js/some-script.min.js') }}
122+
</script>
123+
```
124+
125+
76126
Kirby-specific variables and functions
77127
--------------------------------------
78128

@@ -120,8 +170,8 @@ The few exceptions are:
120170

121171
### Getting config values
122172

123-
- Use the `c(configName, defaultValue)` function in Twig templates to get config values (shortcut for `c::get`).
124-
- Use the `l(configName, defaultValue)` funciton in Twig templates to get language-specific config values or translation strings (shortcut for `l::get`).
173+
- Use the `c__get(configName, defaultValue)` function in Twig templates to get config values (shortcut for `c::get`).
174+
- Use the `l__get(configName, defaultValue)` funciton in Twig templates to get language-specific config values or translation strings (shortcut for `l::get`).
125175

126176
### Kirby Toolkit
127177

0 commit comments

Comments
 (0)