简体   繁体   English

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

[英]How can I format PHP files with HTML markup in Visual Studio Code?

I'm using Laravel so all the views are .blade.php files.我正在使用 Laravel,因此所有视图都是.blade.php文件。 Visual Studio Code won't format the HTML because of the PHP extension.由于 PHP 扩展,Visual Studio Code 不会格式化 HTML。 I removed the "blade" part of the filename, but it still isn't formatting the files properly (via Alt + Shift + F ).我删除了文件名的“刀片”部分,但它仍然没有正确格式化文件(通过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?如何在 Visual Studio Code 中格式化.blade.php文件?

The extension Beautify (from HookyQR) just does it very well.扩展Beautify (来自 HookyQR)做得很好。 Either add PHP, and any other file extension type, to the configuration.将 PHP 和任何其他文件扩展名类型添加到配置中。 As said by Nico , here is an example:正如Nico 所说,这是一个例子:

  1. Go to user settings ( Ctrl + Shift + PUser settings (UI) or Ctrl + , (comma)转到用户设置( Ctrl + Shift + P用户设置(UI) 或Ctrl + , (逗号)

  2. Search for Beautify in the field above.在上面的字段中搜索 Beautify。 And click on "Edit in settings.json" for "Beautify: Config" .然后单击“在 settings.json 中编辑”以获取“美化:配置”

  3. For the "html" section, add "php" and "blade" .对于“html”部分,添加“php”“blade”

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

###Usage ###用法

You can invoke it directly.您可以直接调用它。 Press F1 , and then write Beautify .F1 ,然后写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 )打开 keybindings.json(转到菜单FilePreferencesKeyboard Shortcuts

  2. Click in the above.点击上方。 Open and edit file keybindings.json打开并编辑文件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) (适用于@Peter Mortensen 澄清说明)

blade or blade.php刀片或刀片.php

If you are confused if it's blade or blade.php for the setting!如果您对设置是blade还是blade.php感到困惑! I'll clear it up for you!我来给你解决! It's blade !blade That's vscode keyword for the language!那是该语言的 vscode 关键字!

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) !你可以看到Laravel Blade (blade) The language keyword is in the paratheses!语言关键字在括号中! blade !刀片

Well but how to check!好吧,但如何检查!

Try with blade.php in the settings!尝试在设置中使用blade.php

在此处输入图片说明

Try to beautify尝试美化

在此处输入图片说明

You'll get an asking context menu for what language (html, css, js)!你会得到一个询问什么语言(html、css、js)的上下文菜单!

在此处输入图片说明

So it doesn't works!所以它不起作用!

To really know !要真正知道! Put back blade !放回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!我会说它和 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:这里的美化结果:

在此处输入图片说明

Beautify (from HookyQR) does solve this problem! Beautify (来自 HookyQR)确实解决了这个问题!

Many people add "blade.php" and "php" to the HTML configuration.许多人在 HTML 配置中添加了“blade.php”和“php”。 Beautify does not recognize and manually choose an HTML template. Beautify 无法识别并手动选择 HTML 模板。 Instead, just adding "blade" to the HTML configuration fixes all issues.相反,只需在 HTML 配置中添加“blade”即可解决所有问题。

I think Beautify (by HookyQR) might do the trick.我认为Beautify (by HookyQR) 可能会成功。 Just add 'php' and/or 'blade.php' to the HTML section of its configuration to beautify PHP / Blade view files.只需在其配置的 HTML 部分添加 'php' 和/或 'blade.php' 即可美化 PHP / Blade 视图文件。

It works for me!这个对我有用!

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.如果您想要一些开箱即用的功能,请在支持 Visual Studio Code 的 PHP 文件中添加嵌套 HTML 格式。

It uses all the native settings for html.format , format on save, etc. Give the extension Format HTML in PHP a try.它使用html.format 的所有本机设置、保存时的格式等。试试PHP 中的扩展Format HTML I made it because every other solution made me annoyed, because it didn't work 100%.我做到了,因为其他所有解决方案都让我很恼火,因为它没有 100% 奏效。

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

Andrew Schultz mentioned the extension: " Format HTML in PHP ". Andrew Schultz 提到了扩展名:“ 在 PHP 中格式化 HTML ”。

This is indeed a great extension for format PHP files with HTML markup.这确实是使用 HTML 标记格式化 PHP 文件的一个很好的扩展。 It also formats JavaScript code included in PHP files.它还格式化包含在 PHP 文件中的 JavaScript 代码。

Moreover.而且。 Good news!好消息! The developer is already working successfully to include Laravel blade.php files.开发人员已经成功地包含 Laravel Blade.php 文件。 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.与此同时,我通过手动将语言从Blade切换到 PHP 来格式化 Visual Studio Code 中的 Laravel 刀片文件,然后我使用扩展名“ 在 PHP 中格式化 HTML ”,并使用Ctrl + Alt + F或右键单击。 It works great for me.这对我很有效。

Using a newish feature in php, vscode seems to format and highlight embedded HTML beautifully.使用 php 中的一个新功能,vscode 似乎可以很好地格式化和突出显示嵌入的 HTML。 (php 7.3, released after the question was posted) adds what are called "heredoc" and "neardoc" strings. (php 7.3,在发布问题后发布)添加了所谓的“heredoc”和“neardoc”字符串。 They are well described here: https://andy-carter.com/blog/what-are-php-heredoc-nowdoc .它们在这里有很好的描述: https : //andy-carter.com/blog/what-are-php-heredoc-nowdoc

Basically, you put your HTML in like this:基本上,你把你的 HTML 放在这样的地方:

[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.它在屏幕上看起来很棒。

I use Format HTML in PHP which works well in a file containing both HTML and PHP.我在 PHP 中使用 Format HTML,它在包含 HTML 和 PHP 的文件中运行良好。 However it does not appear to format a 'pure' php file such as a Class.然而,它似乎并没有格式化一个“纯”的 php 文件,比如一个 Class.php 文件。 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.所以我也安装了 Phpfmt,它在处理纯 php 文件时做得很好,但破坏了混合内容文件中的缩进。 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.打开的 php 标签与 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??有什么建议??

This can be done by using HTML formatting for ".blade.php" :这可以通过对".blade.php"使用 HTML 格式来完成:

  1. Open Command Palette ( Ctrl + Shift + P (on a PC))打开命令面板Ctrl + Shift + P (在 PC 上))
  2. Type " Preferences:Open Settings (JSON) " (this is for opening settings.json )输入“首选项:打开设置(JSON) ”(这是打开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.这会将.blade.php文件格式化为 HTML,但不会删除 Blade 片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM