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

This entry was posted in Cool Feature, Newsletter and tagged , , . Bookmark the permalink.
  • Evgeniy Nikonorov

    Cool! But, when this amazing features come to Blade?

    • Eugene Morozov

      What version are you on?
      It seems that both cases are covered now: https://youtrack.jetbrains.com/issue/WI-35123
      https://youtrack.jetbrains.com/issue/WI-26501

      • Evgeniy Nikonorov

        Hi, Eugen.
        I use latest Shtorm.
        Yep. After double check I found that issue is rartial resolved. But! only partial:
        @php /** @var MyClass $a */ @endphp – this works as expected
        @php(/** @var MyClass $a */) – doesn’t work at all…
        Initially i try to use first variant, as both of them are the same features.
        So we should create new issue to support both variants?

        Also there are strange behaviour with doc-block closing.
        To reproduse try this:
        Type @php(/**) – when caret placed after second asterisk and You press space – added closed tag for doc block. Result will be @php(/** */)
        Type @php /** @endphp – do the same. I expect doc block will be closed, but nothing happends.

        Looks like new issue for Elena Shaverdova ))