1

I'm working with CraftCMS, which uses Twig. One of the methods I use to add JS is e.g.

{% js at endBody %}
    console.log('I am JavaScript');
{% endjs %}

I want to have it permanently highlighted as JavaScript, and not have to reinject it every time I open PhpStorm again.

According to https://www.jetbrains.com/help/phpstorm/using-language-injections.html#configure-injection-rules I need to add an injection rule. So I went to

  1. Editor
  2. Language Injections
  3. Add (+)
  4. Generic Twig
  5. (See image)

Language Injection Settings in PhpStorm

What do I have to enter in this window though? I'm a bit lost here. Any help is valuable :)

D3strukt0r
  • 571
  • 1
  • 4
  • 19
  • Did you try clicking the (?) in the corner? Jetbrains's help is pretty useful... https://www.jetbrains.com/help/idea/language-injection-settings-generic-javascript.html – AKX Jul 26 '21 at 13:38
  • @AKX no I didn't, but even after checking out the page, it wouldn't really have helped me :) – D3strukt0r Jul 27 '21 at 06:50

1 Answers1

4

Sure, just add a pattern for matching your custom js Twig tag:

+ twigCustomStatement("js")

https://www.jetbrains.com/help/phpstorm/language-injection-settings-generic-twig.html

P.S. PhpStorm already has a bundled similar rule for script Twig tag. It does exactly the same. The only difference is in the tag name: script versus your js.

enter image description here


The final result (notice the light green background that indicates the Language Injection rule in action + syntax colors for JS code; shows both your tag and bundled one):

enter image description here
(NOTE: copy+pasting in the file, re-opening the file or the whole project/IDE may be needed to have new Injection rule to be applied)

Proof that it's a JavaScript:

enter image description here

LazyOne
  • 158,824
  • 45
  • 388
  • 391
  • Thank you so much. So thorough I have nothing else to say hahaha. I could have guessed that there are existing rules, but I didn't see them. I just had to reopen the files for it to apply. Closing and opening the project didn't help. Thanks – D3strukt0r Jul 27 '21 at 06:48