piernik piernik - 21 days ago 11
Twig Question

Relative paths in twig or absolute in PhpStorm

I'm new to

twig
. I'm using it in PhpStorm and have problem with paths.

Here is my folder structure:

- template /folder/
- main.twig
- common /folder/
- bootstrap.twig
- nav.twig


In
main.twig
I have code:

{% extends "common/bootstrap.twig" %}


When I Ctrl + Click on link PhpStorm opens correct file.

But in
bootstrap.twig
I have to write:

{% include "common/nav.twig" %}


and then Ctrl + Click is not working.
I cannot use relative paths like
nav.twig
or
./nav.twig
- twig throws error.

How can I make twig to use relative paths so go to source would work in PhpStorm?

Answer

Mark your template root folder (template in your case) as Resource Root (either via right click in Project View or via Settings/Preferences | Directories).

https://www.jetbrains.com/help/phpstorm/2016.2/configuring-folders-within-a-content-root.html?search=directories

Files under a folder marked as Resource Root can be referenced relative to this folder.