An extension for Twig that allows to include and extends templates using relative paths.
composer require diasfs/twig-relative-path
use Twig\Extension\RelativePathExtension;
use Twig\Environment;
...
$twig = new Environment($loader);
$twig->addExtension(new RelativePathExtension());
{# layout.html.twig #}
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
{% block content '' %}
</body>
</html>
{# pages/inc/form.html.twig #}
<form>
...
</form>
{# pages/page.html.twig #}
{% extends "../layout.html.twig" %}
{% block content %}
{% include './inc/form.html.twig' %}
{% endblock %}
The resulting html will be the following:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<form>
...
</form>
</body>
</html>
The library is released under the MIT License. See the bundled LICENSE file for details.