Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I format PHP files with HTML markup in Visual Studio Code?

I'm using Laravel so all the views are .blade.php files. Visual Studio Code won't format the HTML because of the PHP extension. I removed the "blade" part of the filename, but it still isn't formatting the files properly (via Alt+Shift+F).

I also tried about five extensions, but none of them do the reformatting.

How can I format .blade.php files in Visual Studio Code?

like image 928
progonkpa Avatar asked Dec 26 '16 11:12

progonkpa


People also ask

How do I arrange PHP code in Visual Studio?

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 .

How do I beautify a HTML file in Visual Studio Code?

To improve the formatting of your HTML source code, you can use the Format Document command Ctrl+Shift+I to format the entire file or Format Selection Ctrl+K Ctrl+F to just format the selected text. The HTML formatter is based on js-beautify.

Can VS Code edit PHP?

PHP in Visual Studio Code. Visual Studio Code is a great editor for PHP development. You get features like syntax highlighting and bracket matching, IntelliSense (code completion), and snippets out of the box and you can add more functionality through community-created VS Code extensions.


1 Answers

The extension Beautify (from HookyQR) just does it very well. Either add PHP, and any other file extension type, to the configuration. As said by Nico, here is an example:

  1. Go to user settings (Ctrl + Shift + PUser settings (UI) or Ctrl + , (comma)

  2. Search for Beautify in the field above. And click on "Edit in settings.json" for "Beautify: Config".

  3. For the "html" section, add "php" and "blade".

    Enter image description here Enter image description here

###Usage

You can invoke it directly. Press F1, and then write Beautify. The auto completion gives you two choices, "Beautify file" and "Beautify selection". Choose the one you need, and it will do the job. That's a straightforward direct way.

Enter image description here

You can also add a keybinding to have a keyboard shortcut. Here is how to do it:

  1. Open keybindings.json (go to menu FilePreferencesKeyboard Shortcuts)

  2. Click in the above. Open and edit file keybindings.json

  3. Add the following into the closed brackets, []

    {        "key": "alt+b",        "command": "HookyQR.beautify",        "when": "editorFocus" } 

    Choose any key you want, and make sure you don't override an existing one. Search first in the left side if it exists or not.

    Enter image description here

Note that all of those things are well documented in the description of the extension.

Extra: Note about blade

(suite to @Peter Mortensen clarification pinpoint)

blade or blade.php

If you are confused if it's blade or blade.php for the setting! I'll clear it up for you! It's blade! That's vscode keyword for the language!

How do you know ?

First if you open the list of languages as by the image bellow:

enter image description here

Write blade

enter image description here

You can see Laravel Blade (blade)! The language keyword is in the paratheses! blade!

Well but how to check!

Try with blade.php in the settings!

enter image description here

Try to beautify

enter image description here

You'll get an asking context menu for what language (html, css, js)!

enter image description here

So it doesn't works!

To really know ! Put back blade! And it will work and beautify directly!

How well it works with blade

The awesome answer to that! Is try it, and see for yourself!

But if you ask me! I'll say it does work as with html! It can be too handy! And you may need to fix some lines! Depending on your expectations and your style!

Here an illustration! I screwed the indentation in purpose

enter image description here

And here the beautification result:

enter image description here

like image 117
Mohamed Allal Avatar answered Sep 18 '22 11:09

Mohamed Allal