Language Injection in PhpStorm

In almost every project, we have a moment where we want to make use of language injection: embedding a small island of a different programming language in our code, typically embedded in a string. Consider embedding an HTML fragment in a PHP literal, a situation we’ve all no doubt have encountered. Here’s an example:

The $htmlFragment variable holds HTML code in which double quotes have to be escaped. If we were using single quotes, we’d have to concatenate strings and make our code even more unreadable. What if PhpStorm could help us out?

This functionality is available in IntelliJ Idea, PyCharm, WebStorm, PhpStorm, RubyMine, and AppCode.

Meet language injections. PhpStorm can recognize these situations and provides us with a full-featured HTML editor to edit the existing string literal:

We can also create a string and use the Inject Language intention to specify the string holds a different language:

A nifty little feature that is! And it also works in all other languages supported by PhpStorm. Let us know your thoughts in the comments below or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

About Maarten Balliauw

Maarten Balliauw is a Developer Advocate at JetBrains, working on .NET tools. He focuses on .NET, Azure, web technologies and application performance. Maarten is a frequent speaker at various national and international events. In his free time, he brews his own beer. Follow him on Twitter or check his personal blog.
This entry was posted in Cool Feature and tagged , , , , , . Bookmark the permalink.