Highlighting js-code inside php file

只谈情不闲聊 提交于 2019-12-30 08:15:12

问题


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> -->

回答1:


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.




回答2:


  • 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>";



回答3:


Might be helpful, but 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;


来源:https://stackoverflow.com/questions/12781037/highlighting-js-code-inside-php-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!