如何在 Visual Studio Code 中使用 HTML 标记格式化 PHP 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41330707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:22:59  来源:igfitidea点击:

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

phplaravelvisual-studio-codecode-formatting

提问by progonkpa

I'm using Laravel so all the views are .blade.phpfiles. 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).

我正在使用 Laravel,因此所有视图都是.blade.php文件。由于 PHP 扩展,Visual Studio Code 不会格式化 HTML。我删除的文件名的叶片部分,但它仍然不能(通过正确格式化文件Alt+ Shift+ F)。

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

我还尝试了大约五个扩展,但没有一个进行重新格式化。

How can I format .blade.phpfiles in Visual Studio Code?

如何.blade.php在 Visual Studio Code 中格式化文件?

回答by Mohamed Allal

The extension beautify just do it very well, either add php and any other file extension type to the config as said above here is an example :

扩展程序 beautify 做得很好,如上所述,将 php 和任何其他文件扩展名类型添加到配置中,这里是一个示例:

  1. go to user settings (CTRL+ SHIFT+ P> user settings (UI) or CTRL+ ,(comma))
  2. search for beautify in the field above. And click on edit on settings.json.
  3. for html section just add php and blade.
  1. 转到用户设置(CTRL+ SHIFT+ P> 用户设置 (UI) 或CTRL+ ,(逗号))
  2. 在上面的字段中搜索 beautify。并点击edit on settings.json
  3. 对于 html 部分,只需添加 php 和blade.

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

OTHERWISE:you can also do it directly, type F1then write beautify, the auto completiongive you two choices beautify selectionor beautify file. Choose the one you need, and it will do the job. that's a straight direct way.

否则你也可以直接做,输入F1然后写beautify自动补全给你两个选择美化选择美化文件选择您需要的那一款,它就能完成这项工作。这是一种直接的方式

enter image description here

在此处输入图片说明

You can alsoadd a keybindingto have a keyboard shortcut, here how to do it:

您还可以添加一个键绑定以获得键盘快捷键,这里是如何做到的:

  1. open keybindings.json (go file>preferences>keyboard shortcuts)
  2. click in above open and edit keybindings.json
  3. add the following into the closed brackets []

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

  1. 打开 keybindings.json(转到文件>首选项>键盘快捷键
  2. 单击上方打开并编辑keybindings.json
  3. 将以下内容添加到右方括号 [] 中

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

choose any key you want, and make sure you don't override and existing one, search first in the left side if it exist or not.

选择您想要的任何键,并确保您没有覆盖和现有的键,如果存在与否,请先在左侧搜索。

enter image description here

在此处输入图片说明

note that all of those things are well documented on the description of the extension.

请注意,所有这些内容都在扩展的描述中得到了很好的记录。

回答by Sam Dan

Beautifydoes solve this problem! Many people add "blade.php" and "php" to HTML config Beautify does not recognize and manually choose HTML template. Instead just adding "blade" to HTML config fix all issues.

美化确实解决了这个问题!许多人将“blade.php”和“php”添加到HTML config Beautify 无法识别并手动选择HTML 模板。只需将“刀片”添加到 HTML 配置即可解决所有问题。

回答by Nico

I think beautifymight do the trick. Just add 'php' and/or 'blade.php' to the HTML section of its config to beautify PHP / Blade view files. Works for me!

我认为美化可能会奏效。只需在其配置的 HTML 部分添加 'php' 和/或 'blade.php' 即可美化 PHP / Blade 视图文件。为我工作!

回答by rifi2k

If you want something that just works out the box, adds the format nested HTML in PHP files support vscode should already have.

如果您想要一些可以直接使用的东西,请在 PHP 文件中添加嵌套 HTML 格式,支持 vscode 应该已经具备的格式。

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

使用 html.format 的所有本机设置、保存时的格式等。试试PHP 中的扩展格式 HTML。我做到了,因为其他所有解决方案都让我很恼火,因为它没有 100% 有效。

回答by Andrew Schultz

I found this extensioncalled Format HTML in PHPthat works well for me.

我发现这个名为Format HTML in PHP 的扩展对我来说效果很好。

回答by TeachMe

This can be done by using html formatting for .blade.php

这可以通过使用 .blade.php 的 html 格式来完成

  1. Open Command Palette(CTRL+Shift+P[on pc])
  2. Type "Preferences:Open Settings (JSON)" (This is for opening settings.json)
  3. Add

    "files.associations": { "*.blade.php": "html", ".tpl": "html" },

  1. 打开命令面板(CTRL+Shift+P[on pc])
  2. 输入“首选项:打开设置(JSON)”(这是打开settings.json
  3. 添加

    "files.associations": { "*.blade.php": "html", ".tpl": "html" },

This will format .blade.php as html but will not remove blade snippet.

这会将 .blade.php 格式化为 html,但不会删除刀片片段。

回答by Gus

Andrew Schultz mentioned the extension: "[Format HTML in PHP][1]" [1]: https://marketplace.visualstudio.com/items?itemName=rifi2k.format-html-in-php. This is indeed a great extension for format PHP files with html markup. It also format js 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 on vscode by switching language manually from blade to php then I use the extension "[Format HTML in PHP][1]" with CTRL+ALT+F or right click. It works great for me.

Andrew Schultz 提到了扩展名:“[在 PHP 中格式化 HTML][1]”[1]:https: //marketplace.visualstudio.com/items?itemName=rfi2k.format-h​​tml-in-php 。这确实是使用 html 标记格式化 PHP 文件的一个很好的扩展。它还格式化包含在 PHP 文件中的 js。而且。好消息!开发人员已经成功地包含 Laravel Blade.php 文件。同时,我通过手动将语言从刀片切换到 php 来格式化 vscode 上的 Laravel 刀片文件,然后我使用扩展名“[在 PHP 中格式化 HTML][1]”和 CTRL+ALT+F 或右键单击。这对我很有效。