本文介绍了您如何缩进HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出我的应用程序生成的HTML.

Given the HTML generated by my application.

function pagination(){
  echo "<ul>\n";

  for($i = 1; $i <= 10; $i++)
    echo "\t<li>...</li>\n";

  echo "</ul>\n";
}
?>
<div>
  <?php pagination(); ?>
</div>

如果我添加另一个容器DIV,这不会产生正确缩进的代码.

If I add another container DIV, this doesn't produce correctly indented code.

该函数是否有解决方案,以某种方式知道要添加多少\ t或空格,或者以某种方式自动缩进html?

Is there any solution for the function to somehow know how many \t 's or spaces to add, or somehow automatically indent the html?

推荐答案

一个令人惊奇的问题.

9个答案和3条评论,而且看起来没有人愿意去阅读问题正文,但是只是重复了标题中的关键字触发的一些福音,这是在stackoverflow祝福的站点上回答问题的最优选方式.

9 answers and 3 comments so far, and looks like nobody bothered to read the question body, but just repeated some gospel triggered by a keyword in the title - a most preferred manner to answer questions on the blessed site of stackoverflow.

但是问题不是那么简单/单层的.
我不得不承认,它本身是模棱两可的.因此,我们必须将其挖掘出来.

Yet the question not that simple/one-layered.
I have to admit, it's ambiguous itself. So, we have to dig it out.

使用模板,伙计.使用模板.唯一的答案.

Use templates, dude. Use templates. The only answer.

当然没有.
PHP不了解HTML,缩进等.
尤其是在尚未准备好HTML的情况下(!)

问题的问题.
为了这个问题而提出的问题.

The key question of the question.
The question for sake of which the question were asked.

其中最难的是.

答案是我完全不同意的答案,呵呵:
尽管标签的相对顺序很重要,但是对于生成的大型HTML,可以将某些块移出行:

And the answer is kind of ones I showed total disagreement with, hehe:
Although relative order of tags is important, for the resulting large HTML it is possible to move some blocks out of row:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Hello</h1>
    <div>
<!-- news list -->
<div>
  <ul>
    <li>1..</li>
    <li>2..</li>
    <li>3..</li>
    <li>4..</li>
  </ul>
</div>
<!-- /news list -->
    </div>
    <div>
<!-- pagination -->
<ul>
  <li>Red</li>
  <li>Green</li>
  <li>Blue</li>
  <li>Black</li>
</ul>
<!-- /pagination -->
    </div>
</body>
</html>

这将使您在有意义的块中具有适当的缩进,但仍使主要的HTML保持顺序.
作为副作用,它将使您的行保持在屏幕上:)

It will let you have proper indention in the meaningful blocks, yet keep the main HTML in order.
As a side effect it will keep your lines on the screen :)

为了在子模板中保持良好的缩进,我强烈建议您使用基于PHP的模板.出于善意,这并不是丑陋的HEREDOC!
这是PHP模板遵循的一条规则:
始终将PHP块保留在左侧.就这样.
要在PHP嵌套块之间保持缩进,只需在< ;?中缩进它们??>

To keep good indentation inside sub-templates, I'd strongly suggest using PHP-based templates. Not ugly HEREDOC for goodness' sake!
Here is only one rule to follow with PHP templates:
always keep PHP blocks to the left side. That's all.
To keep indentation between PHP nested blocks, just indent them inside <? ?>

示例:

<ul>
<? foreach ($thelist as $color): ?>
  <li>
<?   if ($color == $current): ?>
    <b><?=$color?></b>
<?   else ?>
    <a href="?color=<?=$color?>"><?=$color?></a>
<?   endif ?>
  </li>
<? endforeach ?>
</ul>

这将产生正确缩进的HTML,同时在模板中保持HTML和PHP的顺序,从而使开发人员在开发和调试时都更加轻松.

This will produce correctly indented HTML, while keeping order of both HTML and PHP in the template, making developer's life easer both at development and debugging.

请勿听任何人说根本不需要缩进您的代码!".他们只是业余爱好者,而不是真正的开发人员.任何对 debugging 有所了解的人,如果在调试代码时遇到困难,都会告诉您正确的缩进是必不可少的.

Do not listen to anyone who says "no need to indent your code at all!". They are merely hobbyists, not the real developers. Anyone who have an idea of what debugging is, who had hard times debugging their code, would tell you that proper indentation is essential.

这篇关于您如何缩进HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 04:56