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

10

36

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?

Fane answered 26/12, 2016 at 11:40 Comment(2)
Possible duplicate of VSCode Format code command for PHP/HTMLLindemann
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.Fool
H
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

Hellhound answered 20/10, 2017 at 19:3 Comment(9)
This almost works well, it thinks that PHP tags are opening HTML tags though and indents everything below itBukharin
I just added "blade" and not "blade.php" as suggested below by @Sam DanKnighton
Pencil icon, where?Zendejas
@Zendejas 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.Hellhound
Should "blade" in file "settings.json" be "blade.php" or not? Why or why not?Giovannagiovanni
@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!Hellhound
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!Hellhound
This extension is deprecated as it is no longer being maintainedJamilla
@MohammadAghayari the library is not maintained any more true 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 github.com/beautify-web/js-beautify. And if It already works well. Should be ok. Unless there is a better alternative.Hellhound
A
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.

Antisana answered 19/1, 2018 at 11:5 Comment(2)
I added to my user config"beautify.language": { "js": { "type": [ "javascript", "json", "jsonc" ], "filename": [ ".jshintrc", ".jsbeautifyrc" ] }, "css": [ "css", "scss" ], "html": [ "htm", "html", "blade" ] },Halfmast
Doesn't this imply the Blade files should end in ".blade" (and not ".blade.php")?Giovannagiovanni
A
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!

Andeee answered 7/1, 2017 at 15:5 Comment(2)
Re "'blade.php'": Shouldn't it be just "'blade'"?Giovannagiovanni
Why does it work better than the other extensions the OP tried if there is no direct support for Blade files?Giovannagiovanni
V
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%.

Volgograd answered 24/7, 2018 at 13:2 Comment(2)
Which native setting are you referring to?Willodeanwilloughby
@JacobLee read here: <marketplace.visualstudio.com/…>Colwen
K
1

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.

Kaylyn answered 16/2, 2019 at 0:19 Comment(0)
S
1

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

Spiro answered 19/7 at 18:48 Comment(0)
L
0

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

Liquidate answered 17/10, 2018 at 5:27 Comment(1)
Repeat of the previous answer?Giovannagiovanni
E
0

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.

Euraeurasia answered 29/6, 2019 at 5:7 Comment(1)
Why * for the first item, but not the second (".tpl")?Giovannagiovanni
C
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.

Chrominance answered 17/9, 2020 at 21:11 Comment(0)
A
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??

Aboriginal answered 15/2, 2021 at 20:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.