PHP fragment in JS page

I'm writing a project in PHPStorm that uses some JS pages.

On one of the JS pages I want to use a PHP fragment to define variables for the JS code to use so I have a

<?php echo "var test = $variable; \n"; ?>

however PHPStorm reports it as a number of errors the first of which is "statement expected".

Is there a way just to tell it to ignore this PHP fragment and stop it moaning about it? It works fine after all!

Thanks in advance,

Chris.

 

 

0
5 comments
Avatar
Permanently deleted user

Hi,

Unfortunately you cannot inject PHP inside JS files - only JS inside PHP files.

It happens because we cannot inject languages like PHP but can inject HTML, CSS, JavaScript and others available in Settings | Languages & Frameworks | Template Data Languages.

0

Thanks for the response Vladimir.

When you say "you cannot" do you mean your IDE doesn't support it?

It seems to be fairly widely used across the net to pass the value of variables from PHP to JS running on the client PC.

Surely there must be a way to stop PHPStorm inspecting a few lines of code to suppress them being reported as errors when they work fine in practice?

Thanks again!

Chris.

 

 

 

 

 

0

@Chris

Could you please clarify your situation a bit more -- provide a screenshot (whole editor window (well, at very least with file name/icon visible)) of how it all looks like -- the file itself; the warning that IDE gives etc.

1) It's not clear what exactly you are using to have PHP inside JS (and what the exact context is) -- is that a just .js file that gets processed by PHP on a server or something else; is that a whole JS file or just a <script> part  etc -- nuances are possible.

2) It's also important to keep in mind how this IDE provides PHP support etc

Based on the above you most likely misunderstood what Vladimir has stated (what's the correct way of doing this in this IDE)

0

As you can see in the image, the file is a .js file, but it contains a fragment of PHP code used to configure some JS variables. Hope this helps.

Chris.

 

0

That's a correct behaviour. JavaScript parser does not know anything about PHP code. You cannot just tell JS parser "just ignore these few characters/lines" that parser cannot identify as valid. That's how this IDE works .. and that's why it's difficult to support brand-new-super-mega-cool-new-templating-system/syntax -- it has to be parsed properly (by some IDE plugin).

If you need PHP support -- the file MUST be treated as PHP file in IDE -- that's a requirement.

 

Few options:

1) Just add .php extension at the end -- IDE will treat such *.js.php file as PHP with JavaScript inside (instead of default HTML)

Then on your server side (e.g. .htaccess or whatever) you may setup URL Rewrite rule (e.g. "if *.js file is requested and it does not exist but the same *.js.php is available -- use it instead" - 2 lines using mode_rewrite for Apache; single rule for IIS (it's XML based, so will be few lines anyway); similar 2 lines for nginx (I guess))

2) You may give another double extension (e.g. *.php.js or whatever) or some other unique name pattern/extension (e.g. *.php-js) and then associate it with PHP file in "Settings/Preferences | Editor | File Types"

Then you have to tell IDE to use JavaScript as outer language instead of default HTML (the settings that Vladimir has mentioned: "Settings/Preferences | Languages & Frameworks | Template Data Languages"

Depending on file extension used .. you most likely will still need to provide URL Rewriting rules.

As you can see it's the same as #1 (the final behaviour in the end) but slightly longer (more manual configuration) but gives you flexibility in file naming.

 

P.S. Found the actual official article: https://confluence.jetbrains.com/pages/viewpage.action?pageId=53315225

1

Please sign in to leave a comment.