9

I have a file script.js.php. It contains PHP and JavaScript code(the js depends on the php). And it is to be included into a page as js-file.

<script type="text/javascript" src="script.js.php"></script>

Example of script.js.php

<?php
    require_once 'functions.php'
?>
var vars = {
    var1: 'value1',
    var2: 'value2',
    var2: '<?php echo phpFunction(); ?>'
}

Does anybody know, is it possible to make PhpStorm higlight JavaScript code within a PHP file without using script-tag?

Maybe there is some kind of pseudo-tags which wouldn't affect final html/js but make PhpStorm hightlight code "properly", e.g.

<!-- <section language="javascript">--> 
    js goes here 
<!-- </section> -->
2
  • Settings | Template Data Languages -- find your file(s)/folder(s) and assign JavaScript to them (instead of default HTML). Commented Oct 8, 2012 at 11:53
  • @LazyOne. Awesome! Thx. Put this in answer and I'll accept it. Commented Oct 8, 2012 at 11:59

4 Answers 4

9

Settings | Template Data Languages -- find your file(s) or folder(s) and assign JavaScript to them (instead of default HTML).

If you keep your stuff well organized, then you will benefit from keeping all *.js.php files under separate subfolder -- this allows single assignment for a whole folder (and all files inside) instead of multiple assignments for individual files.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that this is a far better solution than adding, say, *.js.php to File Types because then it doesn't inject libraries for some reason. It'll say window is undefined and jQuery is undefined. This way though, with Template Data Languages, you still get code completion with libraries like jQuery or even just DOM like the window object.
9
  • Put your cursor on the JavaScript part
  • Press Alt + Enter
  • Select Inject Language
  • Select JavaScript

For example:

<?php
echo "<script>/* CURSOR HERE*/
    alert('Hello World');
    </script>";

8 Comments

That's different -- he has different approach -- no echo is used, as you can see, so there is no place for manual language injection
thx, but it forbidden to use <script> tag within js file. as far as I know.
@LazyOne then there is no such way. He should either put his *.php in the settings to be highlighted as .js files, or he should put his script as a string so he can use this method.
@RuslanPolutsygan It was just a string example. It doesn't matter what is inside the string. You can highlight every string as any syntax.
@Jelmer this won't work for me because I don't want to call php functions inside strings.
|
2

If you use the hard-coded "JS" heredoc, it'll know it's Javascript and give you autocomplete for that snippet:

$javascript_code = <<<JS
  function test(){
  console.log('hello!');    
}
JS;

Comments

0

Use PHPDocs

Directly above of the String that contains the Javascript code write:

/* @lang JavaScript */ 

PHPStorm will automaticly highlight the Javascript code in your PHP-String.

You can Also write

/* @lang MySQL */ 
/* @lang CSS */

to highlight CSS or MySQL Code within your PHP Code within PHPStorm

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.