如何在Visual Studio Code中用HTML标记格式化PHP文件? [英] How can I format PHP files with HTML markup in Visual Studio Code?

查看:531
本文介绍了如何在Visual Studio Code中用HTML标记格式化PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel,因此所有视图均为.blade.php文件.由于PHP扩展,Visual Studio Code无法格式化HTML.我删除了文件名的刀片"部分,但仍然无法正确格式化文件(通过 Alt + Shift + F )

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.

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

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

推荐答案

扩展名美化(来自HookyQR)非常出色.将PHP和其他任何文件扩展名类型添加到配置中.正如Nico所说的 ,这是一个示例:

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. 转到用户设置( Ctrl + Shift + P 用户设置(UI)或 Ctrl + (逗号)
  2. 在上面的字段中搜索Beautify.然后单击在settings.json中编辑" ,以获取美化:配置" .
  3. 对于"html" 部分,添加"php" "blade" .

  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".

用法

您可以直接调用它.按 F1 ,然后输入美化. 自动完成为您提供了两个选择:美化文件" 美化选择" . 选择所需的人,它将完成工作.这是直接直接方法.

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.

您还可以添加键盘绑定以具有键盘快捷键.这是操作方法:

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

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

  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, []

**{<br>
       "key": "alt+b",<br>
       "command": "HookyQR.beautify",<br>
       "when": "editorFocus"<br>
}<br>**

选择所需的任何键,并确保不覆盖现有的键.如果不存在,请首先在左侧搜索.

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.

请注意,所有这些内容在扩展说明中都有详细记录.

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

(适合@Peter Mortensen阐明的观点)

(suite to @Peter Mortensen clarification pinpoint)

如果您对设置是blade还是blade.php感到困惑!我会帮你清理的! blade!这是该语言的vscode关键字!

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!

您怎么知道?

首先,如果您按如下图所示打开语言列表:

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

写刀片

您可以看到Laravel Blade (blade)!语言关键字在括号中! 刀片

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

但是,如何检查!

尝试在设置中使用blade.php

尝试美化

您将获得一个询问上下文菜单,询问哪种语言(html,css,js)!

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

所以它不起作用!

要真正知道!放回blade! 它将直接起作用并且可以美化!

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

最棒的答案!尝试一下,自己看看吧!

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

但是,如果您问我!我会说它确实可以和html一起使用!可能太方便了!而且您可能需要修复一些线!根据您的期望和风格!

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

这里是美化的结果:

这篇关于如何在Visual Studio Code中用HTML标记格式化PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆