Language Injection in PhpStorm

Posted on by Maarten Balliauw

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

Comments below can no longer be edited.

12 Responses to Language Injection in PhpStorm

  1. Oink says:

    June 4, 2013

    Wow, didn’t know this before. Thanks will use this in future.

  2. Andreas Möller says:

    June 5, 2013

    Very helpful feature!

  3. William says:

    June 5, 2013

    A cool feature that I use a lot. Unfortunately, I run into a lot of false positive errors with some languages, especially SQL. (“Select * from $db.$table” is invalid, but shouldn’t be, and the inspection that causes the error can’t be disabled.)

    It may also be helpful to know that heredoc strings can be automatically detected. <<< JS is always treated as Javascript, <<< HTML is HTML, << Language Injections.

  4. Crantzbea says:

    June 7, 2013

    How do I make the lightbulb go away in the code editor? I have tried disabling all inspections.

    • Andriy Bazanov says:

      June 12, 2013

      It’s not possible to disable light bulb.

      But will be possible in v7.

  5. Jan R says:

    July 12, 2013

    try ALT+Enter -> “Replace quotes” with this example:

    $string="foo\nbar";

    result:
    $string='foo\nbar';

    (Sorry for double post because of html tags)

    but should be:
    $string='foo'."\n".'bar';

    greetz

    • MadDocNC says:

      September 6, 2013

      Well, it seems to be not so complicated function. It also transforms
      “$foo” to ‘$foo’ not ”.$foo.”

      • MadDocNC says:

        September 6, 2013

        sorry,used tags…
        “foo $bar” to ‘foo $bar’ not to ‘foo ‘.$bar

  6. Syntax highlighting of PHP inside JavaScript (and other languages) | JetBrains PhpStorm Blog says:

    November 20, 2013

    […] one file. If there is only one or two occurrences of these small, embedded language islands, using Language Injections is the way to go. But what if we have a lot of […]

  7. 複数言語が入り交じったファイルのシンタックスハイライト(PHP内のJavaScriptなど) | JetBrains ブログ says:

    November 21, 2013

    […] 時々一つのファイルに複数のプログラミング言語を書かなければいけないことがあります。1つや2つの小さなコードブロックであれば言語インジェクションを使うことをおすすめします。では別の言語がたくさんある場合はどうしたらいいでしょう? […]

  8. Lanre says:

    May 22, 2014

    When using Edit HTML Fragment Webstorm insert \n in the src string for each line break. Is it a way to ask Webstorm to automatically visually format those string by indenting the code? Maybe by splitting the string and add indent? The problem is that the editor is very usefull but when looking at the src it’s a mess to read, we end up with super long string, visually unformated and afaik we need to reedit each string to actually see it nicely.

  9. Language Injections Enhancements in PhpStorm 8 | JetBrains PhpStorm Blog says:

    September 9, 2014

    […] JavaScript variable, or database queries inside a string. With PhpStorm 8, we’ve enhanced the Language Injections feature to make working with different language fragments a […]

Subscribe

Subscribe for updates