Link to home
Start Free TrialLog in
Avatar of razorwire
razorwire

asked on

<script> tag on external .js.php file

PROBLEMS:
I want to mix PHP and JS in:
file.js.php

In my text editor (Textmate) syntax styling does NOT work without the <script> tags. ie)
<script>
alert('help');
</script>

file.js.php will only run without the <script> tags. ie)
alert('help');

When including file.js.php I do NOT want to do:
include('file.js.php');

When including file.js.php I want to do:
<script type="text/javascript" src="/file.js.php" /></script>

I do NOT want to have to manually add/remove the <script> tags from file.js.php just to mix PHP and JS!

QUESTIONS:
Why are the <script> tags NOT accepted by web browsers in an external JS file (This would solve my problem)?
Is it NOT recommended to mix PHP and JS?
Suggestions?
Avatar of hielo
hielo
Flag of Wallis and Futuna image

>>Why are the <script> tags NOT accepted by web browsers in an external JS file
Because a javascript file is meant to have javascript code, NOT html code/markup. <script> is NOT javascript code.
Doesn't Textmate have a way to manually select which code highlighting to use?
Avatar of razorwire
razorwire

ASKER

I figured it out.

I just can comment out the <script> tags and it works as desired:
//<script>
alert('help');
//</script>

The 2 lines with the script tags don't work perfectly but this is a good workaround. =)

@hielo
I know it isn't javascript code... but I thought the language could at least ignore the <script> tags or something..

@Kraimir
Textmate does have a way to manually set which code highlighting is in use... but there are still problems when mixing PHP and JS... Plus changing code highlighting is a manual change as far as I know.
Just a tip - you can actually let PHP parse .js files also. On Apache, you can append the AddType handler for PHP as follows:

AddType x-httpd-php .php .js

This will then parse .js files for any PHP markup.
The solution I thought of earlier is kind of a pain:
- Syntax highlighting doesn't match up where the /*<script>*/ and /*</script>*/ tags are.
- You have to type a bunch of extra stuff =(
- ie) /*<script>*/alert('<?= $php_variable; ?>');/*</script>*/
- That might seem worse than no syntax highlighting at all depending on how many $php_variable items you use...

On the good side though:
- you NEVER have to manually change your code highlighting...

Maybe I can NOT have what I want and JavaScript and PHP are NOT intended to be integrated together... There are other scripting languages too and I imagine I may have the same problems with those...  But it PHP should provide syntax that text editors will recognize when using several different languages together!

The syntax I would like to use to incorporate ALL languages together so you can have correct styling of several languages incorporated together in a single PHP file WITH AUTOMATIC CORRECT STYLING on ANY text editor is:

<?php echo 'PHP'; ?> OR <? echo 'PHP'; ?>
<?js alert('JS'); ?>
<?html <h1>HTML</h1> ?>
<?client_side_language_154 print "CLIENT_SIDE_LANGUAGE_154"; ?>

SO THEN:
/*<script>*/alert('<?= $php_variable; ?>');/*</script>*/

BECOMES:
<?js alert('<?= $php_variable; ?>'); ?>

ANOTHER THEORETICAL EXAMPLE (html.js.php):
<?html
    <script><?js
        alert('<?php
            echo 'this is a file with correct code styling for 3 differnt scripting languages'
            . ' and it gets displayed correctly in a web browser.';
        ?>');
    ?></script>
?>

Let me explain:
- This notation may require changes with the PHP processor and with text editors.
- Correct code styling with several languages would then be possible in a single PHP file.
- The PHP processor would take out lines like <?html, AND <?js AND <?php automatically so they don't get sent to the web browser.
- <? and ?> would work similar to { and }. Except that the TEXT EDITOR in use knows what language is between <?html <?js <?php ?> ?> ?>

Thoughts?

@kingmonty
I could do the the solution you proposed.  However:
-  the problem I run into here is that my text editor (Textmate) uses a completelly different code styling for PHP code blocks when the coding style is set to Javascript. ( NOT such a big deal but things should be consistent. )
- Also, I don't want to pretend .js files are .php files.
I will make my previous post a suggestion to PHP or Zend or something...

But for NOW... after working with this issue for a while... The way to get the most accurate syntax highlighting for multiple languages in a single file.js.php for Textmate is to just put the following on the first line of an external javascript file:
//<script>

Then just deal with the highlighting differences for the php parts embedded in that same file...
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@hielo
The reason I want to suggest this to PHP is because the ONLY way you can ensure that the correct code styling is done is if there is some kind of "separator" between differenent languages mixed in the same file.

With most things we are ok.. there are already separators...
ie) <?  ?>
<script> </script>

However, there are cases where a text editor will be unable to realize the language chage is present because a separator will NOT be present... like if something is in quotations... Below is the easiest example I can think of.

ie)file.html.php
<? $variable = '<p>Don\'t ' . $_GET['verb'] . '</p>'; ?>
- incorrect html code styling.
- requires \' or fiddling with the quotations.

ie)file.abc
<? $variable = ?><?html <p>Don't ?><? . $_GET['verb'] . ?><?html </p> ?><? ; ?>
- correct html code styling.
- No need to escape or fiddle with the apostrophe in "Don't."

NOTE: The syntax I used in this example is different from what I mentioned previously but it may be better.
NOTE: Two separate syntaxes (<? or <?= ) would ONLY be needed for the SERVER side language (PHP).

My suggestion may be ignored by the PHP community but I think people would love to see somthing fill in gap when it comes to code stylling AND mixing different languages together...

@hielo
I will accept your answer as it basically states that what I want can NOT be done currently.
Thank you!
PHP just got back to me... apparently there is a way to do something similar if not the same as what I was proposing on the PHP side by using NOWDOC or HEREDOC syntax.  However, I have not found a text editor that has correct code styling with the HEREDOC syntax.  I haven't even found an official list of HEREDOC keywords such as: HTML, JAVASCRIPT, SQL, etc..

ie)
<script>
alert('help'); // SHOULD HAVE IDENTICAL CODE STYLING TO THE OTHER MATCHING LINE
</script>
<?
echo <<<JAVASCRIPT
alert('help'); // SHOULD HAVE IDENTICAL CODE STYLING TO THE OTHER MATCHING LINE
JAVASCRIPT;
?>

In the above example the matching lines do NOT have identical code styling on:
TextMate,
vim
Zend Studio for Eclipse
UltraEdit

They do have identical code styling on the following text editor however... Other code styling tests fail on this editor:
EditPlus

It seems like ALL the text editors don't care what keyword you put after <<< and they just assume that it you will only need HTML code styling within the HEREDOC block.  Perhaps I have a configuration error... but I don't think you should have to even configure anything for your text editor if it already has the php file extension!!!  

Please let me know if there is a text editor out there with good code styling within HEREDOC syntax that is dependant on what KEYWORD you use after <<<. Maybe I will have to open another question!

Note:
Heredoc support was added in PHP 4.
Nowdoc support was added in PHP 5.3.0.

See:
http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
http://us3.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

MY PHP FEATURE REQUEST:
http://bugs.php.net/bug.php?id=46046
I might be able to go to sleep now...

Textmate can do HEREDOC code styling as long as you update the PHP bundle to the latest one. See:
http://macromates.com/

To update the PHP bundle after installing textmate change "Python" to "PHP" in the terminal commands on the following page:
http://blog.macromates.com/2006/updating-a-bundle/
Problem now is that within a HEREDOC you are unable to use NON-DOUBLE-QUOTED PHP code within the HEREDOC syntax...