Features Releases

Twig handling improvements

PhpStorm provides native support for Twig templates, and with version 2017.3 it just got better.

The Twig formatting mechanism now handles complex structures properly. It provides correct indentation for your code blocks and preserves it when copying and pasting code fragments between files.

See it for yourself! Invoke the actions search by pressing Shift+Command+A (Shift+Control+A on Windows) and search for the Reformat code action:

reformat

We’ve also implemented the long-awaited language injections for Twig templates. To try them out, place the caret inside an opening {% block %} tag and invoke the Inject language or reference intention action by pressing Alt+Enter. Then, simply choose the desired language from the list and start writing your code:

injection_basic

Language injection can also occur automatically when an injection rule is triggered. Two Twig-related injection rules are built into PhpStorm 2017.3, which allow you to inject JavaScript code.

The first rule is triggered as soon as you name a block javascript:

injection_named_block

And the second one – inside a custom script tag:

injection_custom_block

It doesn’t just stop here, of course. If you navigate to Settings | Editor | Language Injections, you’ll be able to customize these injections to your liking as well as create new ones. You can learn more about working with code injections in our help article.

Your JetBrains PhpStorm Team
The Drive to Develop

image description