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

Commit 3d36e97

Browse files
author
Florens Verschelde
committed
Document namespace feature
1 parent a5fa6d7 commit 3d36e97

File tree

5 files changed

+47
-8
lines changed

5 files changed

+47
-8
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ Documentation
5151
-------------
5252

5353
- [Twig templating guide](doc/templating.md)
54+
- [Complete options documentation](doc/options.md)
5455
- [How errors are displayed (or not)](doc/errors.md)
5556
- [Using your own functions in templates](doc/functions.md)
56-
- [Complete options documentation](doc/options.md)
57+
- [Twig support in Kirby plugins](doc/plugins.md)
5758

5859

5960
Credits

doc/errors.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
kirby-twig: How errors are displayed (or not)
2-
=============================================
1+
[kirby-twig] How errors are displayed (or not)
2+
==============================================
33

44
With PHP templates, most errors are shown directly in the page. Things are a bit different with Twig: if an error is not suppressed, the template will *not* be rendered at all, and you end up with an error page.
55

@@ -13,7 +13,7 @@ If `c::get('debug')` is true:
1313
- A nice error page is shown, with an excerpt of the faulty template code.
1414

1515
<figure>
16-
<img src="doc/errorpage.png" width="770" alt="">
16+
<img src="errorpage.png" width="770" alt="">
1717
</figure>
1818

1919
## In production

doc/functions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
kirby-twig: Using your own functions in templates
2-
=================================================
1+
[kirby-twig] Using your own functions in templates
2+
==================================================
33

44

55
If you need to expose PHP functions or classes to your Twig templates, you can list them with those two options:

doc/options.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
kirby-twig: Options documentation
2-
=================================
1+
[kirby-twig] Options documentation
2+
==================================
3+
4+
## Basic options
35

46
```php
57
// REQUIRED: activate Twig plugin
@@ -12,12 +14,20 @@ c::set('twig.usephp', true);
1214
// Kirby URI of a page to render when there is a Twig error in production
1315
// For instance 'error/system'. Falls back to c::get('error').
1416
c::set('twig.error', '');
17+
```
18+
19+
## Customizing the Twig environment
1520

21+
```
1622
// List of additional functions that should be available in templates
1723
c::set('twig.env.functions', ['myCustomFunction']);
1824
1925
// List of classes that can be instantiated from templates (with the `new()` function)
2026
c::set('twig.env.classes', ['SomeClass']);
27+
28+
// Define a directory as a Twig namespace, that can be used as:
29+
// {% include '@mynamespace/something.twig' %}
30+
c::set('twig.namespace.mynamespace', kirby()->roots()->index().'/mydirectory');
2131
```
2232

2333
## Advanced

doc/plugins.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
[kirby-twig] Twig support in Kirby plugins
2+
==========================================
3+
4+
If you’re developing a Kirby plugin to share it with the Kirby community, it’s probably not a great idea to make it rely on kirby-twig. You should probably provide PHP templates or snippets.
5+
6+
For project-specific plugins, though, if your project is using Twig, you might want to keep your plugin-related templates and snippets in your plugin’s folder. Here are a few config tricks that might help.
7+
8+
```php
9+
<?php
10+
// site/myplugin/myplugin.php
11+
12+
/**
13+
* Add a Twig namespace for our plugin's templates
14+
* Allows including with {% include '@myplugin/mytemplate.twig' %}
15+
*/
16+
c::set('twig.namespace.myplugin', __DIR__ . '/templates');
17+
18+
/**
19+
* Expose functions from our plugin to the Twig environment
20+
*/
21+
$twigFunctionNames = array_merge(
22+
c::get('twig.env.functions', []),
23+
['myPluginFunction', 'myOtherFunction']
24+
);
25+
c::set('twig.env.functions', $twigFunctionNames);
26+
```
27+
28+
See [Using your own functions in templates](functions.md) for more details about exposing functions, static methods and classes to templates.

0 commit comments

Comments
 (0)