Format HTML in PHP
Formatting for the HTML code in PHP files.
This extension provides formatting for the HTML code in PHP files. This way this works is this extension runs right before the save action which triggers any other registered PHP formatting extensions so you are free to have one and this will not get in the way or block it.
- Uses VSCode configurations for HTML formatting
- Uses VSCode format on save setting
- Custom keybinding for anytime formatting
- Right click context menu option on PHP files
- Command Palette option for formatting
There are multiple issues and Stackoverflow posts about not being able to format the HTML in PHP files and none of the solutions proposed anywhere really worked 100% so I decided to fix it.
Issues, Ideas, Feature Requests? Go ahead and add them. I'm down to help, add, or fix anything because I know this is badly needed for a lot of PHP / WordPress devs and was a major annoyance for me.
Feel free to have nested HTML in .php files now, it will format as you are expecting.
Using This Extension
CTRL + ALT + F
You can change this if desired through the standard Keyboard Shortcuts (File > Preferences > Keyboard Shortcuts) option screen the name is "Format HTML in PHP".
Within a PHP file you can right click and there is a menu option to Format HTML in PHP.
Format On Save
Turn on format on save either globally or scoped to PHP.
Here is the list of native vscode settings I pass to JS Beautify which control how your HTML will be formatted. You can change any of these to configure how the HTML will be formatted.
"html.format.extraLiners": "head, body, /html",
See the extension installation guide for details.
- JS Beautify to stable 1.12.0
- JS Beautify to stable 1.10.3
- JS Beautify to stable 1.10.1
- JS Beautify to stable 1.9.0
- Documentation update about HTML settings - #17
- JS Beautify bump to latest beta
- Updating to the new JS Beautify version. If anyone notices any issues let me know!
- Upstream fixes #14 - Don't format <?php tags
- Don't add empty newline inside HTML script tags when html.format.endWithNewline is true.
- Add HTML wrap attributes option.
- README and instructions overhaul.
- Hotfix - Saving a file without format on save enabled would replace all the code in the file with absolutly nothing. Oops...
- Fixes #11 - Fixes the cursor jumping to the end of the file on formatting.
- Fixes #8 - Adding workbench command for Format HTML in PHP.
- Add right click menu option for Format HTML in PHP.
- Add keybind Ctrl + Alt + F to format HTML in a PHP file.
- Update code for checking on PHP scoped formatOnSave setting to use native vscode method.
- Update README to be more clear about how formatOnSave settings should be configured.
- Fixes #4 - Check for formatOnSave being set under the nested setting for just PHP files, and not just checking the main setting.
- Fixes #5 - Duplicate of #4
- Fixes #2 - Forgot about the fact when I switched to the save hook, it would run everytime no matter what. Duh.
- Figured out the root cause was some of my defaults which I removed so I was able to add back in all the configurable fields again.
- Had to remove a few options which were causing problems formating in certain situations.
Being a better neighbor!
- Change from registering as a formatting provider (which languages can only have one active), to doing the work onBeforeSave. This way if users wanted to activate an extension that formatted the actual PHP we can all live in harmony. Most likely a formatting extension like phpcbf is going to register as a format provider so I proactively moved my work out of the way, because I'm always looking out and don't want a user to have to decide "which extension to keep".
- Added the ability for all of the HTML formatting settings as well as the editor settings for tabs vs spaces to be inherited and used by the formatter.
- Gutted all the things that didn't need to be there.
- Fixes #1