.js.php files: Syntax highlighting and auto completion
Answered
Hi,
I've a CodeIgniter project with some views that includes files like that:
- Extension: .js.php
- Content: Javascript inside <script> tags with <?php ?> code inside
- Screenshot: https://i.imgur.com/1hz2aB0.png
Result: PhpStorm doesn't recognize correctly the syntax and the auto-completion isn't working.
I'm actually using Netbeans and it works perfectly in these cases, is it possible to do it with PhpStorm?
Please sign in to leave a comment.
Hi there,
Why do you need <script> tag if double extension suggests that it meant to be a pure JavaScript file (as if file was named "file.js")? I mean -- is that framework requirement or something you have added yourself (e.g. it may depend on the way how this file will be used later -- will it be called directly via URL or included via typical PHP's include/require)?
What PhpStorm does right now (lots of users have asked for this behaviour) is that it treats such file with double ".js.php" extension as "PHP with JavaScript as outer language" instead of default "PHP with outer HTML".
You can override it at any time -- just use "Settings/Preferences | Languages & Frameworks | Template Data Languages" and specify "HTML" for that file (so it treats <script> tag correctly).
Or just get rid of <script> tag and whole file will be treated as JavaScript (if you can, of course). Or get rid of double extension (e.g. use another one).
Hi Andriy, thx for the answer.
I've this files in the "views" folder together with the php/html file:
/views/form.php + /views/form.js.php
At the end of the form.php I've an 'include' of the .js.php file. I need it to be .js.php because it's useful the possibilty to use php functions and variables inside the JS file (to get base_url, a variable send from the controller, etc...)
I tried your suggestion, setting all the /views folder template to HTML and it seems to work, thx!