diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..b361b73 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,37 @@ +Kirby Twig Plugin: Change log +============================= + +v3.0.0 +------ + +**Breaking changes:** + +- Removed the `twig.env.classes` option and `new()` Twig function. +- Twig’s template cache is now disabled by default (enable with `c::set('twig.cache', true);`). +- Error reporting: the `twig.error` config key is now ignored. Instead, the site’s main error page (whose URI is `error` by default) will be used in some specific situations. See `doc/errors.md` for details. +- Namespace and class names (and sometimes methods) have changed (again); there is now a `Kirby\Twig\Plugin` class which will act as a stable API, while other implementation details may change. + +Deprecated (still working): + +- `twig.env.functions` in favor of `twig.function.myFunction`; +- `twig.env.filters` in favor of `twig.filter.myFilter`; +- `twig.env.namespace.xyz` in favor of `twig.namespace.xyz`. + +v2.x +---- + +See on GitHub: + +- [v2.1.2](https://github.com/fvsch/kirby-twig/releases/tag/v2.1.2) +- [v2.1.1](https://github.com/fvsch/kirby-twig/releases/tag/v2.1.1) +- [v2.1.0](https://github.com/fvsch/kirby-twig/releases/tag/v2.1.0) +- [v2.0.2](https://github.com/fvsch/kirby-twig/releases/tag/v2.0.2) +- [v2.0.1](https://github.com/fvsch/kirby-twig/releases/tag/v2.0.1) +- [v2.0.0](https://github.com/fvsch/kirby-twig/releases/tag/v2.0.0) + +v1.x +---- + +- [v1.3.0](https://github.com/fvsch/kirby-twig/releases/tag/v1.3.0) +- [v1.2.0](https://github.com/fvsch/kirby-twig/releases/tag/v1.2.0) +- [v1.0.0](https://github.com/fvsch/kirby-twig/releases/tag/v1.0.0) diff --git a/doc/functions.md b/doc/functions.md index a800cec..881de86 100644 --- a/doc/functions.md +++ b/doc/functions.md @@ -4,8 +4,8 @@ Using your own functions in templates If you need to expose PHP functions (or static class methods) to your Twig templates, you can list them with those options: -- `twig.env.functions` -- `twig.env.filters` +- `twig.function.*` +- `twig.filter.*` As with any option in Kirby, you should define these options in your `site/config/config.php`. Let’s show how each option works. @@ -13,7 +13,23 @@ As with any option in Kirby, you should define these options in your `site/confi Exposing a function ------------------- -For example if you have a custom function defined in your own plugin file (`site/plugins/myplugin.php`): +The expected syntax for these configuration options is: + +```php +// In site/config/config.php: +c::set('twig.function.myFunctionName', $someFunction); +``` + +Where: + +- `myFunctionName` is any name you want (only letters and underscores), and is the name that will be available in your Twig templates. +- `$someFunction` can be a string, or a Closure. + +Let’s use more tangible examples. + +### Using a function name (string) + +If you have a custom function defined in a plugin file (e.g. `site/plugins/myplugin.php`): ```php newInstanceArgs($args); } return new $name; -} - -c::set('twig.env.functions', ['makeInstance']); +}); ``` Then in Twig templates: ```twig -{% set coolThing = makeInstance('VeryCoolThing') %} +{% set coolThing = new('VeryCoolThing') %} ``` diff --git a/doc/options.md b/doc/options.md index 9fe81a0..8f30ce7 100644 --- a/doc/options.md +++ b/doc/options.md @@ -2,19 +2,6 @@ Options documentation ===================== -Basic options -------------- - -```php -// REQUIRED: activate Twig plugin -c::set('twig', true); - -// Should we use .php templates as fallback when .twig -// templates don't exist? Set to false to only allow Twig templates -c::set('twig.usephp', true); -``` - - Customizing the Twig environment -------------------------------- @@ -23,18 +10,26 @@ Customizing the Twig environment // {% include '@mynamespace/something.twig' %} c::set('twig.namespace.mynamespace', kirby()->roots()->index().'/mydirectory'); -// List of additional functions that should be available in templates -c::set('twig.env.functions', ['myCustomFunction']); +// Expose an existing function in templates +c::set('twig.function.myfunction', 'myCustomFunction'); -// List of additional functions that should be available as Twig filters -c::set('twig.env.filters', ['myCustomFilter']); +// Expose an existing function in templates as a filter +c::set('twig.filter.myfilter', 'myCustomFilter'); ``` +See [Using your own functions in templates](functions.md) for details about Twig functions and filters. + +Note: Kirby Twig 2.x used a `twig.env.functions` config, accepting an array of function names. This option is deprecated, but will still work in Kirby Twig 3.x. + Advanced -------- ```php +// Should we use .php templates as fallback when .twig +// templates don't exist? Set to false to only allow Twig templates +c::set('twig.usephp', true); + // Use Twig’s PHP cache? // Disabled by default (starting from 2.2). // Enabling Twig's cache can give a speed boost to pages with changing diff --git a/doc/plugins.md b/doc/plugins.md index 4554ab3..15d1c18 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -63,11 +63,8 @@ c::set('twig.namespace.myplugin', __DIR__ . '/templates'); /** * Expose functions from our plugin to the Twig environment */ -$twigFunctionNames = array_merge( - c::get('twig.env.functions', []), - ['myPluginFunction', 'myOtherFunction'] -); -c::set('twig.env.functions', $twigFunctionNames); +c::set('twig.function.myPluginHelper', 'myPluginHelper'); +c::set('twig.function.myOtherHelper', 'myOtherHelper'); ``` See [“Using your own functions in templates”](functions.md) for more details about exposing functions, static methods and classes to templates.