6

I noticed that the syntax/php.vim file on my ubuntu machine has a php_htmlInStrings option. I can turn this option on to display HTML syntax highlighting within strings in my php files, which is great. I would also like to do javascript syntax highlighting within strings in a php file. Does anybody know if this can be done and if so how can I do it?

edited - added extra possibilities

I should also mention that I would be happy with a solution where i have to parse all my javascript strings though a php function before outputting the result. This might get around the problem suggested by conner below where vim has trouble deciding if the string contains javascript. for example:

$js = "some regular text which is not javascript##now vim has
detected that this part is javscript##back to regular text";
parse($js);
function parse($str)
{
    return str_replace('##', '', $str);
}

The reason I would be happy to do this is because I will probably be incorporating a html/css/js variable minifier into my project which will be doing substitutions on strings anyway.

Of course if there is a vim-specific equivalent character for ## which will not show up in the source code and would not need to be filtered out then this would be preferable...

re-edit 2

As per conner's solution below, the desired effect can be achieved like so:

$js = "<script>some javascript</script>";

(with :let php_htmlInStrings=1 in vim). If somebody can show me the vim script required to get the javascript syntax highlighting to show up in the following string then I will award them the answer:

$js = /*<script>*/"some javascript"/*</script>*/;

2 Answers 2

2
+50

I think the general problem with this is that vim needs a way to differentiate between the javascript and HTML highlighting. In HTML files, vim determines this based on the <script></script> tags within it to apply the javascript highlighting. If you put <script></script> tags in your string you'll see that this is the case. However, if you take those away then vim has no way of knowing if the content in your string is HTML or javascript. You could remedy this by editing adding something to signify that it's javascript that hopefully wouldn't effect the resultant code, but this is tricky. You can see where the HTML file is setting the <script></script> tag specification on line 167 of $VIMRUNTIME/syntax/html.vim. It looks like this:

syn region  javaScript start=+<script[^>]*>+ keepend end=+</script>+me=s-1 contains=@htmlJavaScript,htmlCssStyleComment,htmlScriptTag,@htmlPreproc
Sign up to request clarification or add additional context in comments.

4 Comments

hmm yeah i thought of that as well. i parse all my javascript strings though a minifying function in php anyway so i could put a character at the start and end of each string to signify js encoding. do you have any ideas as to how i could do it if so? (i'll add this fact into the question)
Well, I don't think it's a very good solution, but if you used brackets around your JS then you could use the same code above but modify the start and end fields to start=/{/ keepend end=/}/me=s-1
i could use something like $js = (("some javascript")); - this complies fine in php. only thing is that it would be picked up by nested function calls. for example: print_r(("blah" == $a) ? 'yes' : 'no'); ...more code...print_r('and again'.(($a == "blah") ? 'yes' : "no"));
i could pretty safely go for $js = /*<js>*/"some javascript"/*<js>*/ or something similar though :)
0

Have you tried this php.vim syntax file?

1 Comment

i just tried it then and it does not work. should it work straight away or do i need to set an option to get it to show javascript syntax highlighting within my php strings?

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.