diasfs/twig-relative-path

Twig extension to work with relative paths

1.0.1 2024-10-01 13:27 UTC

This package is auto-updated.

Last update: 2024-12-01 00:21:12 UTC


README

An extension for Twig that allows to include and extends templates using relative paths.

Instalation

composer require diasfs/twig-relative-path

Initialization

use Twig\Extension\RelativePathExtension;
use Twig\Environment;

...

$twig = new Environment($loader);
$twig->addExtension(new RelativePathExtension());

Example

{# 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>

License

The library is released under the MIT License. See the bundled LICENSE file for details.