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

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

问题描述

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

我还尝试了大约五个扩展程序,但没有一个会重新格式化.

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

解决方案

扩展

###用法

您可以直接调用它.按F1,然后写美化.自动完成为您提供了两个选择,美化文件"美化选择".选择您需要的,它就能完成工作.这是一种直接的方式.

您还可以添加键绑定以拥有键盘快捷键.操作方法如下:

  1. 打开 keybindings.json(转到菜单 FilePreferencesKeyboard Shortcuts)

  2. 点击上方.打开并编辑文件 keybindings.json

  3. 将以下内容添加到右括号中,[]

    <代码>{键":alt+b",命令":HookyQR.beautify",何时":editorFocus"}

    选择您想要的任何键,并确保您不会覆盖现有的键.如果存在与否,首先在左侧搜索.

请注意,所有这些内容都在扩展的描述中得到了详细记录.

额外:刀片注意事项

(适用于@Peter Mortensen 澄清说明)

blade 或blade.php

如果您对设置是blade 还是blade.php 感到困惑!我来给你解决!这是刀片!这是该语言的 vscode 关键字!

你怎么知道的?

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

写刀片

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

好吧,但如何检查!

在设置中尝试使用 blade.php

尝试美化

你会得到一个询问什么语言(html、css、js)的上下文菜单!

所以它不起作用!

要真正知道!放回刀片!而且它会直接工作和美化!

刀片效果如何

这个问题的答案真棒!试试看,自己看看!

但是如果你问我!我会说它确实像 html 一样工作!它可能太方便了!您可能需要修复一些线路!取决于您的期望和您的风格!

这是一个插图!我故意搞砸了缩进

这里是美化结果:

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?

解决方案

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

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

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:

Write blade

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!

Try to beautify

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

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

And here the beautification result:

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

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