简体   繁体   English

PhpStorm 中 .htm 文件中的 PHP 代码样式

[英]PHP Code Style in .htm File in PhpStorm

If I add some php code to a .html or .htm file in PhpStorm, the code style for that bit of php code is NOT the same as in .php file.如果我在 PhpStorm 中的.html.htm文件中添加一些 php 代码,那段 php 代码的代码样式与.php文件中的代码样式不同。

For instance, here is the same code in two different file formats:例如,这是两种不同文件格式的相同代码:

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

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

How can I get the php code segment inside of the .htm to look like it does in a .php file?如何让.htm的 php 代码段看起来像在.php文件中一样?

UPDATE更新

The reason I want php code styling within an htm file is because I am using October CMS .我想要在 htm 文件中设置 php 代码样式的原因是因为我使用的是October CMS October CMS uses a .htm file extension which is divided into three section.十月 CMS使用 .htm 文件扩展名,该文件扩展名分为三个部分。 One of those section is for php code .这些部分之一是用于 php 代码 What I want to know is how can I get the php content code which resides in the htm file to use my php code styles within PhpStorm.我想知道的是如何获取驻留在 htm 文件中的 php 内容代码以在 PhpStorm 中使用我的 php 代码样式。

In order for PhpStorm to recognize and highlight PHP code in any file such file MUST be associated with "PHP File" file type and have PHP icon next to file name.为了让 PhpStorm 识别和突出显示任何文件中的 PHP 代码,此类文件必须与“PHP 文件”文件类型相关联,并且文件名旁边有 PHP 图标。

By default .htm / .html files are associated with "HTML Files" file type.默认情况下, .htm / .html文件与“HTML 文件”文件类型相关联。


Few possible solutions:几个可能的解决方案:

  1. Change extension of existing file to be .php将现有文件的扩展名更改为.php
  2. Use .phtml file extension -- which is commonly used extension for such cases (in general denotes that this file is a "template" -- a HTML file mixed with PHP instructions)使用.phtml文件扩展名——这是此类情况下常用的扩展名(通常表示此文件是“模板”——混合了 PHP 指令的 HTML 文件)
  3. Associate .htm (or whatever else extension you want) with "PHP File" file type..htm (或您想要的任何其他扩展名)与“PHP 文件”文件类型相关联。

    For that: Settings/Preferences | Editor | File Types为此: Settings/Preferences | Editor | File Types Settings/Preferences | Editor | File Types Settings/Preferences | Editor | File Types -- find "PHP File" and add appropriate pattern (eg *.htm ). Settings/Preferences | Editor | File Types ——找到“PHP 文件”并添加适当的模式(例如*.htm )。 IDE will ask if you want to remove such pattern from existing/default "HTML File" to this "PHP File". IDE 会询问您是否要将此类模式从现有/默认“HTML 文件”删除到此“PHP 文件”。

    Possible downside: some HTML inspections may not be triggered (relaxed) in PHP file.可能的缺点:在 PHP 文件中可能不会触发(放松)某些 HTML 检查。

    NOTE: Such association is IDE-wide and affects ALL projects.注意:这种关联是 IDE 范围的,会影响所有项目。


The best/most neutral solution overall is #2 -- use dedicated file extension for such files ( *.phtml is a most commonly used).总体上最好/最中立的解决方案是#2——为此类文件使用专用的文件扩展名( *.phtml是最常用的)。

PHP needs to be in a .php file. PHP 需要在一个.php文件中。 It's changing colour because your text editor is telling you that the browser will treat it as text if it's in a .html file.它正在改变颜色,因为您的文本编辑器告诉您,如果它位于.html文件中,浏览器会将其视为文本。

Rename it to .php and you'll be fine.将其重命名为.php可以了。

Two file formats are not identitiques . 两种文件格式不相同。 You must change the extension of the second file with the extension .php . 您必须使用扩展名.php更改第二个文件的扩展名。

.htm is not supported to interpret PHP syntaxes. 不支持.htm解释PHP语法。

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

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