PHP script to Generate Javascript & Highlighting in Zend Studio
-
18-09-2019 - |
Question
I have a PHP script that I am using to generate some rather complicated javascript for a web application. It is necessary that this script be generated by PHP for scalability reasons.
Here is my problem:
if I name the file
myscript.js
, then I have to use htaccess to tell the webserver to pipe this through PHP (not preferrable)if I name the file
myscript.php
, then Zend Studio will highlight all the code as HTML instead of javascript.
What I am doing now is simply naming the script myscript.php
, and wrapping all the code in <script>
tags, then doing include('myscript.php')
instead of the preferred: <script src='myscript.php'>
.
I could thwart this whole problem if only I could tell Zend Studio to highlight this file as though it were a *.js
file. Is there a way of accomplishing this?
Currently I am using versions 5.5 (for Windows, at work) and 6.1 (for Linux, at home).
Solution
Is there not a way to open your file with the Javascript editor ?
Something like right click on the file's name, then "Open With > Javascript editor" ?
Well, I've just tried with Zend Studio (I don't find the version number, but it's a pretty recent one, downloaded less than one month ago -- and Eclipse based)
To see "Javascript editor" in the "Open with" list, you might have to add a file association between .php and "Javascript". To do that, follow these steps :
- Windows > preferences
- general > editors > file associations
- add *.php to the list ("add" button) if it's not already in it (it is not by default, it seems)
- when you click on *.php, you see two entries in the list at the bottom :
- php editor
- text editor
- add "javascript editor" to this list (the other "add" button, next to the list) -- don't set it as default (as you only want a couple of .php files opened with it, not all of them)
- click OK to validate
Now, in the files list, you should be able to right click on a PHP file, and "open with > javascript editor"
And your file should be highlighted like a JS one, and not as PHP.
I'm not sure Zend studio will remember the fact that one file has to be opened with JS editor, though : you might always have to use "open with" for the file you are interested in.
Hope this helps ;-)
OTHER TIPS
I've found a very easy hack to cause zend to think of the PHP file as javascript:
// <script type="text/javascript">
alert("Hi");
// </script>
But I think Pascal's approach is cleaner...