R
R
Roman Kutenko2015-01-20 12:35:48
PHP
Roman Kutenko, 2015-01-20 12:35:48

How to change directory for Twig templates?

Hi all.
How to change template call directory in Twig?

\theme
->layouts\admin.twig
->admin\login.twig

{% extends "layouts/admin.twig" %}

{% block content %}
    Content of the page...
{% endblock %}

at the same time, it looks for a template in \theme\admin\
if you specify ../layouts/admin.twig then I get an error

Twig_Error_Loader [ Error ]:
Looks like you try to load a template outside configured directories (../layouts/adminpage.twig) in "login.twig" at line 1.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Shamanov, 2015-01-20
@Sky4eg

twig.sensiolabs.org/doc/api.html#basics

M
My joy, 2015-01-20
@t-alexashka

require_once '/path/to/lib/Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('/theme');
$twig = new Twig_Environment($loader, array());

And now the twig will search for the rest of the templates relative to this path. now paths like this should work correctly:
{% extends "layouts/admin.twig" %} // theme/layouts/admin.twig

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question