使用 Vim 正确缩进 HTML 和 PHP

发布于 2024-10-20 14:19:29 字数 2034 浏览 2 评论 0原文

我已经使用 Vim 一段时间了,但我无法在 PHP 文件中获得正确的 HTML 缩进。

例如,我想要的是每个子项比其父项多缩进一个选项卡,如下所示。

<?php
if(isset($sports)) {
    //Do something
?>
<div>
    <label>Uniform Size</label>
    <ul>
        <li class="left"><label for="s" class="small">S</label><input type="radio" name="size[]" value="S" id="s" class="radio" /></li>
        <li class="left"><label for="m" class="small">M</label><input type="radio" name="size[]" value="M" id="m" class="radio" /></li>
        <li class="left"><label for="l" class="small">L</label><input type="radio" name="size[]" value="L" id="l" class="radio" /></li>
        <li class="left"><label for="xl" class="small">XL</label><input type="radio" name="size[]" value="XL" id="xl" class="radio" /></li>
    </ul>
</div>
<?php
}
?>

使用 PHP- Correct-Indent 脚本,代码会被格式化如下所示:

<?php
if(isset($sports)) {
    //Do something
?>
<div>
<label>Uniform Size</label>
<ul>
<li class="left"><label for="s" class="small">S</label><input type="radio" name="size[]" value="S" id="s" class="radio" /></li>
<li class="left"><label for="m" class="small">M</label><input type="radio" name="size[]" value="M" id="m" class="radio" /></li>
<li class="left"><label for="l" class="small">L</label><input type="radio" name="size[]" value="L" id="l" class="radio" /></li>
<li class="left"><label for="xl" class="small">XL</label><input type="radio" name="size[]" value="XL" id="xl" class="radio" /></li>
</ul>
</div>
<?php
}
?>

即使使用缩进的 HTML(然后我将 PHP 代码添加到其中),缩进也会被忽略,移动 HTML 代码的新行根本不会有任何缩进。

那么,有什么方法可以使用 Vim 获得我想要在 PHP 文件中处理 HTML 的缩进格式吗?

I've been using Vim for a while, and I can't get proper HTML indentation working in PHP files.

For example, what I want is for each child to be indented one tab more than it's parent, as shown below.

<?php
if(isset($sports)) {
    //Do something
?>
<div>
    <label>Uniform Size</label>
    <ul>
        <li class="left"><label for="s" class="small">S</label><input type="radio" name="size[]" value="S" id="s" class="radio" /></li>
        <li class="left"><label for="m" class="small">M</label><input type="radio" name="size[]" value="M" id="m" class="radio" /></li>
        <li class="left"><label for="l" class="small">L</label><input type="radio" name="size[]" value="L" id="l" class="radio" /></li>
        <li class="left"><label for="xl" class="small">XL</label><input type="radio" name="size[]" value="XL" id="xl" class="radio" /></li>
    </ul>
</div>
<?php
}
?>

Using the PHP-correct-Indent script, the code results in being formatted as follows:

<?php
if(isset($sports)) {
    //Do something
?>
<div>
<label>Uniform Size</label>
<ul>
<li class="left"><label for="s" class="small">S</label><input type="radio" name="size[]" value="S" id="s" class="radio" /></li>
<li class="left"><label for="m" class="small">M</label><input type="radio" name="size[]" value="M" id="m" class="radio" /></li>
<li class="left"><label for="l" class="small">L</label><input type="radio" name="size[]" value="L" id="l" class="radio" /></li>
<li class="left"><label for="xl" class="small">XL</label><input type="radio" name="size[]" value="XL" id="xl" class="radio" /></li>
</ul>
</div>
<?php
}
?>

Even with indented HTML which I then add PHP code to, the indentation is ignored, moving new lines of HTML code without any indentation at all.

So, is there any way that I can get the indentation format that I want working with HTML within PHP files, using Vim?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

把时间冻结 2024-10-27 14:19:29

这仍然困扰着我。我只是刚刚决定最好的解决方法(对我个人而言)是这样的:

:set filetype=html

然后突出显示您的文本并点击=。繁荣! HTML 格式化成功。 (我知道这并不理想,但至少它有效。)

This still bothers me. I only just decided that the best work-around (for me personally) is this:

:set filetype=html

And then highlight your text and hit =. BOOM! HTML formatting succes. (Not ideal, I know, but at least it works.)

睫毛溺水了 2024-10-27 14:19:29

Vim Wiki 上有一组 vimrc 指令,名为 Better indent support for PHP with HTML,它将根据块使用正确的插件。

还有一个 Vundle/Pathogen 插件,它使用相同的代码,但更容易安装并保持 .vimrc 干净。

Pathogen

cd ~/.vim/bundle
git clone https://github.com/captbaritone/better-indent-support-for-php-with-html.git

Vundle

放入 .vimrc

Bundle 'captbaritone/better-indent-support-for-php-with-html'

在 vi​​m 中运行

:BundleInstall

There is a set of vimrc instructions on the Vim Wiki called Better indent support for PHP with HTML that will use the correct plugin depending on the block.

There is also a Vundle/Pathogen Plugin that uses the same code but is easier to install and keeps your .vimrc clean.

Pathogen

cd ~/.vim/bundle
git clone https://github.com/captbaritone/better-indent-support-for-php-with-html.git

Vundle

Place in .vimrc

Bundle 'captbaritone/better-indent-support-for-php-with-html'

Run in vim

:BundleInstall
冷夜 2024-10-27 14:19:29

在认真研究了所有解决方案之后,我发现了这个插件:

http:// www.vim.org/scripts/script.php?script_id=604

似乎解决了我的问题!!!!

After looking really really hard into all solutions, I found out this plugin:

http://www.vim.org/scripts/script.php?script_id=604

It seems to have solved my problems!!!!!

夜唯美灬不弃 2024-10-27 14:19:29

对我来说,如果我先执行 :set ft=html 然后执行 :set syn=php ,效果会很好。

For me it works good if I first do :set ft=html and then :set syn=php.

友谊不毕业 2024-10-27 14:19:29

在 php+html 中,我发现以下内容对我有好处。

:set ft=html # Change the file type to html
=G # to indent all lines 
:set ft=phtml # Change the file type to phtml
=G # to indent all php lines

In php+html I found the following is good for me.

:set ft=html # Change the file type to html
=G # to indent all lines 
:set ft=phtml # Change the file type to phtml
=G # to indent all php lines
Spring初心 2024-10-27 14:19:29

php- Correct-indenting 只关心你的 PHP,并假设 HTML 的可读性不重要。 XML 缩进器可以很好地定位标签,但无法缩进 的内容。处理指令进行匹配。也许有一个缩进脚本可以理解 PHP 编程语言的类 C 语法和 [X][HT]ML 模板化的标记语言,但我还没有遇到过 - 抱歉。

尽管如此,即使在 php- Correct-indenting 破坏它之前,我还是想摆弄你的示例中的缩进!

元素位于外部 if 语句内,但我无法从缩进中看到这一点。我建议类似:

<?php if(isset($sports)) { ?>
    <?php
        // Do something
    ?>
    <div>
        <label>Uniform Size</label>
        <ul>
            <li>etc. etc.</li>
        </ul>
    </div>
<?php } ?>

php-correct-indenting only cares about your PHP, and assumes the readability of the HTML is of no interest. An XML indenter would position the tags nicely, but wouldn't be able to indent the contents of a <?php> processing instruction to match. Maybe there is an indentation script that understands both the C-like syntax of PHP the programming language and [X][HT]ML the markup language being templated, but I've never met one yet - sorry.

Still, I'd like to fiddle with the indenting in your example even before php-correct-indenting mauled it! The <div> element is inside an outer if-statement, but I have no way to see that from the indenting. I'd suggest something like:

<?php if(isset($sports)) { ?>
    <?php
        // Do something
    ?>
    <div>
        <label>Uniform Size</label>
        <ul>
            <li>etc. etc.</li>
        </ul>
    </div>
<?php } ?>
此岸叶落 2024-10-27 14:19:29

我发现这个解决方案要好得多。
http://www.vim.org/scripts/script.php?script_id=1120

支持 HEREDOC html 样式。这在我的代码中经常出现。
顺便说一句:它比旧版本有更多版本(脚本 ID 604,alex 在上面发布)

i found this solution is much better.
http://www.vim.org/scripts/script.php?script_id=1120

supporting HEREDOC html style. which occur frequently in my code.
BTW:it's has more versions than the old one (script id 604, alex posted it above)

踏雪无痕 2024-10-27 14:19:29

在你的 .vimrc 中:

:function IndentPHPHTML()
:  set ft=html
:  normal gg=G
:  set ft=php
:endfunction

使用 ctrl-shift-L (或其他)来缩进

nnoremap <C-S-l> :call IndentPHPHTML()<cr>

inside your .vimrc:

:function IndentPHPHTML()
:  set ft=html
:  normal gg=G
:  set ft=php
:endfunction

use ctrl-shift-L (or whatever) to indent

nnoremap <C-S-l> :call IndentPHPHTML()<cr>
小红帽 2024-10-27 14:19:29

经过几天的寻找解决方案后,没有任何效果,最后这成功了,将其添加到您的 vimrc

au BufEnter,BufNew *.php :set filetype=html

After searching for days for the solution ,nothing worked and finally this worked,add this to your vimrc

au BufEnter,BufNew *.php :set filetype=html
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文