Reputation: 1366
Is it possible to get Visual Studio Code to format mixed HTML and PHP code on Windows Visual Studio Code version 0.7.1?
Upvotes: 74
Views: 246586
Reputation: 17
Till the date, the only solution I found to format is using AI.
I'm using Github Copilot, selecting what I want to format and in the line editor enter "fix indentation" and voilá, it really gets the task.
Hope this solves your problem like it does with me.
Upvotes: -1
Reputation: 717
Update 2024-07-17
I think this can now be done in VS Code without an extension. If you press Ctrl
+ Shift
+ P
, then select "Change Language Mode", then type "PHP". You should now see the PHP option.
I'm using version 1.74.2
Upvotes: -1
Reputation: 579
I installed
Prettier for HTML, CSS, and JavaScript files
PHP Intelephense for PHP files
I followed the instructions for each plugin but found I had to additionally edit settings.json manually in order to get them to work together.
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[php]": {
"editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
}
The settings use Intelephense as the formatter for PHP files and use Prettier as the formatter for all other files.
Now I use Shift + Alt + F to format the files like everybody else.
Upvotes: 48
Reputation: 2199
press ctrl + shift + p
and type >user settings.json
and paste this lines:
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
},
"[php]": {
"editor.defaultFormatter": "bmewburn.vscode-intelephense-client",
"editor.formatOnSave": true,
},
[javascript]
means it for javascript language
"editor.defaultFormatter"
means you choose a default formatter for javascript
"editor.formatOnSave": true
means it will format your document after saving the file.
Upvotes: 1
Reputation: 41
i finally found one that works decently to format html+php code. The extension is called "All-in-one PHP support" by devsense.com. Hope it helps
Upvotes: 2
Reputation: 23
After installed Extensions. Settings > Search >formatting and enable 'Format On Save' then edit setting.json file as below
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[php]": {
"editor.defaultFormatter": "bmewburn.vscode-intelephense-client",
"editor.formatOnSave": true
},
"editor.formatOnSave": true
Save >Restart IDE this worked for me.. :)
Upvotes: 1
Reputation: 19613
It's been more than half a decade since I first wrote this answer. The extensions to which I originally linked are abandoned, and Visual Studio Code's intrinsic PHP support hasn't improved, which is disappointing. The only decent extension still standing of which I'm aware is PHP Intelephense, which uses a freemium model: basic features are free, and a lifetime license is $12 USD as of writing.
The free version of Intelephense supports code formatting with the usual shortcuts (Alt + Shift + F on Windows and Linux, ⌥⇧F on macOS). Visual Studio Code continues to lack built-in support for PHP code formatting and will direct you to the extension marketplace if you attempt to format PHP without an appropriate extension installed.
Visual Studio Code has pretty awesome PHP support. What it lacks is covered by extensions. A quick search reveals at least three (1, 2, and 3) that claim to support PHP formatting.
They mostly seem to use the standard shortcut of Alt + Shift + F on Windows/Linux, with varying shortcuts on Mac. If you're on Mac, give ⌥⇧F a try.
Upvotes: 102
Reputation: 1387
Go to extension in vs code:
then write: category:formatters php
press enter. you will see all available formatter. at the moment install the one you like.
then right click inside any opened php file chose format document from the popup menu.
Upvotes: 5
Reputation: 381
I use bmewburn.vscode-intelephense-client and it works perfectly (other than if there's an syntax error).
There is a premium version; I don't know how well that works.
The Format HTML in PHP extension that many other people mention does work, except there are some alignment issues, and you have to format the HTML and the PHP separately.
phpcbf is also a good choice, except it requires more customization, and it's a bit more complicated to figure out. That could be a good thing or a bad thing, depending on what you want.
Upvotes: 5
Reputation: 81
I'm coding in Laravel and I am using Format HTML In PHP for formatting the HTML code among the PHP code. It's working perfectly.
Upvotes: 4
Reputation: 51
I've tried just about every formatting extension for Visual Studio Code, and I don't think there is a single one that can handle correctly/consistently formatting of mixed PHP/HTML pages.
Upvotes: 4
Reputation: 881
For the best format setting for mixed PHP, HTML, and JavaScript code, just use 'PHP CS FIXER'.
And then use this simple configuration on your setting.json
file:
"php-cs-fixer.executablePath": "${extensionPath}\\php-cs-fixer.phar",
"[php]": {
"editor.defaultFormatter": "junstyle.php-cs-fixer",
"editor.formatOnSave": true
},
"php-cs-fixer.rules": "@PSR2",
"php-cs-fixer.formatHtml": true,
Upvotes: 18
Reputation: 2165
Add the extension Format HTML in PHP to Visual Studio Code.
Or search in extensions with "format HTML in PHP".
After reload, use Shift + Alt + F.
Upvotes: 2
Reputation: 199
The problem with most of the solutions is that they all are registered as formatting providers and within Visual Studio Code you can only run one formatter on a save for a specific file type.
So one may get you all the HTML, CSS, JavaScript code, but leave out the PHP code. Or if you use a regular PHP formatter there isn't one that exists that does the HTML correctly.
I went ahead and made an extension that runs before the save hook and isn't registered as a PHP formatter, so it will do all the HTML with js-beautify and then you can use something like PHPCS + PHPCBF to format the HTML. So it's basically, as far as I'm concerned, the best solution currently available.
Format HTML in PHP on the Visual Studio Code marketplace.
Upvotes: 8