34

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
progonkpa
  • 3,590
  • 9
  • 31
  • 50
  • Possible duplicate of [VSCode Format code command for PHP/HTML](https://stackoverflow.com/questions/32236030/vscode-format-code-command-for-php-html) – Cees Timmerman Aug 02 '17 at 09:15
  • VS Code has HTML Intellisense out-of-the box, in order to use this plugin for your goal go to main menu - File / Preferences / Settings, then add in your User settings this: "files.associations": {"*blade.php": "html"} It will work for any template extension, twig, etc. It start working even without restart the VS Code. – vinsa Feb 01 '18 at 17:12

9 Answers9

64

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

yaitloutou
  • 1,691
  • 19
  • 23
Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
  • 2
    This almost works well, it thinks that PHP tags are opening HTML tags though and indents everything below it – Brian Leishman Mar 30 '18 at 21:20
  • 1
    I just added "blade" and not "blade.php" as suggested below by @Sam Dan – Adrian Smith Jun 12 '18 at 12:40
  • 1
    Pencil icon, where? – Jonny Oct 29 '19 at 04:56
  • @Jonny things changed from that time! vscode updated it's settings management interface! I will update! But what you need to do is `CTRL + SHIFT + P > user settings (UI)` then click on `edit on settings.json`. – Mohamed Allal Oct 29 '19 at 19:12
  • Should `"blade"` in file "settings.json" be `"blade.php"` or not? Why or why not? – Peter Mortensen Jun 13 '20 at 13:44
  • @PeterMortensen it need to be blade and not blade.php! blade.php will not work! How do you know! if you change to blade.php when you call the command beautify! You'll get asked what to use html or js or css! With blade it works! Also if you click and open the list of supported languages! If you write blade! You'll find it Laravel blade (blade)! The key is in the parantheses! – Mohamed Allal Jun 17 '20 at 11:13
  • How well it works with blade files! I'm not sure! I haven't used laravel in a long time now! But i just checked! It seems the same as with html! There was some unappreciated return to lines! I generally prefer to indent myself! But when you are not the one that wrote it! It come too handy! And you can may be fix some lines if it doesn't seems nice to you! But i'll say it works well! I'll update the answer! And thank you for clearing up what can be confusing! – Mohamed Allal Jun 17 '20 at 11:18
  • This extension is deprecated as it is no longer being maintained – Mohammad Aghayari Jul 11 '22 at 10:40
  • @MohammadAghayari the library is not maintained any more true https://github.com/HookyQR/VSCodeBeautify. But it does use js-beautify library that is used internally by vscode for it's formatters. Which is still being maintained https://github.com/beautify-web/js-beautify. And if It already works well. Should be ok. Unless there is a better alternative. – Mohamed Allal Jul 12 '22 at 22:56
10

Beautify (from HookyQR) does solve this problem!

Many people add "blade.php" and "php" to the HTML configuration. Beautify does not recognize and manually choose an HTML template. Instead, just adding "blade" to the HTML configuration fixes all issues.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sam Dan
  • 169
  • 2
  • 6
  • I added to my user config"beautify.language": { "js": { "type": [ "javascript", "json", "jsonc" ], "filename": [ ".jshintrc", ".jsbeautifyrc" ] }, "css": [ "css", "scss" ], "html": [ "htm", "html", "blade" ] }, – jmartsch Aug 28 '18 at 18:20
  • Doesn't this imply the Blade files should end in "`.blade`" (and not "`.blade.php`")? – Peter Mortensen Jun 13 '20 at 14:03
7

I think Beautify (by HookyQR) might do the trick. Just add 'php' and/or 'blade.php' to the HTML section of its configuration to beautify PHP / Blade view files.

It works for me!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nico
  • 71
  • 1
2

If you want something that just works out of the box, add the format nested HTML in PHP files support Visual Studio Code should already have.

It uses all the native settings for html.format, format on save, etc. Give the extension Format HTML in PHP a try. I made it because every other solution made me annoyed, because it didn't work 100%.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rifi2k
  • 199
  • 1
  • 6
0

I found this extension called Format HTML in PHP that works well for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrew Schultz
  • 4,092
  • 2
  • 21
  • 44
0

Andrew Schultz mentioned the extension: "Format HTML in PHP".

This is indeed a great extension for format PHP files with HTML markup. It also formats JavaScript code included in PHP files.

Moreover. Good news! The developer is already working successfully to include Laravel blade.php files. In the meantime I format Laravel blade files in Visual Studio Code by switching language manually from Blade to PHP then I use the extension "Format HTML in PHP" with Ctrl + Alt + F or right click. It works great for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gus
  • 111
  • 1
  • 7
0

Using a newish feature in php, vscode seems to format and highlight embedded HTML beautifully. (php 7.3, released after the question was posted) adds what are called "heredoc" and "neardoc" strings. They are well described here: https://andy-carter.com/blog/what-are-php-heredoc-nowdoc.

Basically, you put your HTML in like this:

[php code.....]
echo
<<<HTML
   <div>
     <h1>This is a headline</h1>
    <p> this is a paragraph. lorem ipsum lorem ipsum blah black </p>
   </div>
HTML;
[^ don't forget the ;  follow with more php if you want]

it looks just great on screen.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Charles Goodwin
  • 584
  • 5
  • 8
0

I use Format HTML in PHP which works well in a file containing both HTML and PHP. However it does not appear to format a 'pure' php file such as a Class. So I have also installed Phpfmt, which does a great job with the pure php file but wrecks the indenting in a file with mixed content. Here's an example of what I get:

<?php
if (!$logged_in) {
    $login_form = 'data-toggle="modal" data-target="#Modal1"';
    $href = '#';
} else {
    $login_form = '';
    $href = 'members';
}
?>

The opening php tag is correctly aligned with the html. 'I see that under 'Supported Transformations' it says:

AlignPHPCode:   Align PHP code within HTML block.

But as you can see from the above, it's not working for me. Any suggestions??

Amit Verma
  • 8,660
  • 8
  • 35
  • 40
Mike Heath
  • 51
  • 2
  • 8
-1

This can be done by using HTML formatting for ".blade.php":

  1. Open Command Palette (Ctrl + Shift + P (on a PC))
  2. Type "Preferences:Open Settings (JSON)" (this is for opening settings.json)
  3. Add
    "files.associations": {
            "*.blade.php": "html",
            "*.tpl": "html"
    },
    

This will format .blade.php files as HTML, but it will not remove Blade snippets.

TeachMe
  • 535
  • 1
  • 5
  • 16