This library extends Twig with an tag to catch and handle exceptions:
{% try %}
<li>{{ throws_exception() }}</li>
{% catch %}
{# The exception can be accessed using `e` #}
User Error: {{ e.message }}
{% endcatch %}
- Twig >=2.14 and Twig >=3.0
- PHP >=7.4
The recommended way to install this loader is via Composer:
composer require gglnx/twig-try-catch
You can install this library as extension in:
require_once '/path/to/vendor/autoload.php';
$twig = new \Twig\Environment($loader);
$twig->addExtension(new \Gglnx\TwigTryCatch\Extension\TryCatchExtension());
This extension is extracted from Grav CMS. See the file headers and LICENSE
file for copyright information.