PHP injection into a Javascript file


I'm sure this must be possible, but I can't seem to figure out how to set this up (PhpStorm 8.0.1):

I've got a ZF2 project. One of the views is a generated javascript file (javascript.js) which has some PHP injected into it using this format:

function SomeJSFunct(){
var label = '<?php echo $this->translate('My Label Text');?>';

I'd expect PHPStorm to recognice this language injection, but it doesn't... It treats is as a literal string, which is OK for variables, but when I start putting conditional PHP statements in, the highlighting all goes to pan (see screenshot)

I took a look in Language Injections, but I don't see an option to inject PHP into Javascript...

Advice appreciated! Many thanks.

Screen Shot 2014-09-22 at 11.28.00.png
Comment actions Permalink

Hi there,

Yes, it's possible, but it requires some additional moves.

In general:

  1. To have PHP support, such file MUST be associated with PHP file type in IDE instead of JavaScript
  2. Once done, you have to assign "JavaScript" as outer language instead of default "HTML"

The easiest way for #1 is to give such file:

  • double extension (e.g. "javascript.js.php")
  • .. or some custom extension (e.g. "javascript.jsphp" .. or "javascript.pjs" -- just as an examples) and then associate such new extension with "PHP files" in "Settings | File Types" (will affect all projects as this is an IDE-wide setting)

Surely, you can just associate just this file with PHP (via the same "Settings | File Types"), but this will treat ALL files with such names in ALL projects as PHP -- don't think this is what you want.

For #2 -- "Settings | Template Data Languages" -- find your file there as assign "JavaScript" in right column.


Comment actions Permalink

Hi Andriy,

Actaully that makes a lot of sense - my actual view file is 'javascript.phtml' which I've renamed to .js for example's sake. So I think your solution will work jsut fine for me.

Many thanks!


Please sign in to leave a comment.