diasfs/twig-relative-path

Twig extension to work with relative paths

1.0.0 2023-07-28 22:41 UTC

This package is auto-updated.

Last update: 2024-05-29 00:38:12 UTC


README

An extention 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.