如何正确缩进 PHP/HTML 混合代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1155799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to properly indent PHP/HTML mixed code?
提问by James Skidmore
When mixing PHP and HTML, what is the proper indentation style to use? Do I indent so that the outputted HTML has correct indentation, or so that the PHP/HTML mix looks properly formatted (and is thus easier to read)?
混合使用 PHP 和 HTML 时,要使用的正确缩进样式是什么?我是缩进以使输出的 HTML 具有正确的缩进,还是使 PHP/HTML 混合看起来格式正确(因此更易于阅读)?
For example, say I have a foreachloop outputting table rows. Which one below is correct?
例如,假设我有一个foreach循环输出表行。下面哪一项是正确的?
PHP/HTML mix looks correct:
PHP/HTML 混合看起来正确:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
Outputted HTML looks correct:
输出的 HTML 看起来正确:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
I've found that when I run into this situation (quite frequently), I don't have a standard style to use. I know that there may not be a "correct" answer, but I'd love to hear thoughts from other developers.
我发现当我遇到这种情况时(非常频繁),我没有标准的样式可以使用。我知道可能没有“正确”的答案,但我很想听听其他开发人员的想法。
采纳答案by chaos
The PHP and the HTML should each be indented so that they are correct with respect to themselves in source form, irrespective of each other and of outputted form:
PHP 和 HTML 都应该缩进,以便它们在源格式中相对于自己是正确的,而不管彼此和输出形式:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table>
回答by mpen
I often pondered this question too, but then I realized, who cares what the HTML output looks like? Your users shouldn't be looking at your HTML anyway. It's for YOUto read, and maybe a couple other developers. Keep the sourcecode as clean as possible and forget about what the output looks like.
我也经常思考这个问题,但后来我意识到,谁在乎 HTML 输出是什么样的?无论如何,您的用户不应该查看您的 HTML。供您阅读,也许还有其他几个开发人员阅读。保持源代码尽可能干净,忘记输出的样子。
If you need to debug the output, use Chrome Developer Tools, Firebug, or even F12 Tools.
如果您需要调试输出,请使用 Chrome 开发人员工具、Firebug 甚至 F12 工具。
回答by TJ L
I generally put openingphp tags at the beginning of the line, but indent whatever is inside the tags to match the html formatting. I don't do this, however, for simple echo statements since I use short-open tags. I think it makes simpler it when browsing through the file to find all the declarations.
我通常将打开的php 标签放在行的开头,但缩进标签内的任何内容以匹配 html 格式。但是,对于简单的 echo 语句,我不会这样做,因为我使用了短开标签。我认为在浏览文件以查找所有声明时会更简单。
<table>
<? foreach ($foo as $bar): ?>
<tr>
<? foreach ($bar as $baz): ?>
<td><?=$baz?></td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
回答by Treb
- Direct answer to your question: If you need to read the HTML output often, it might be a good thing to output well indented HTML. But the more common case will be that you need to read your php source code, so it is more important that the source is easily readable.
- Alternative to the two options you mentioned: See chaos'or tj111'sanswer.
- Better still in my opinion: Don't mix HTML and php, use a template engine instead.
回答by Etzeitet
You can always use a bit of whitespace too to help readability. Building on chaos' indentation:
您也可以随时使用一些空格来提高可读性。建立在混乱的缩进上:
<table>
<?php foreach ($rows as $row): ?>
<tr>
<?php if ($row->foo()): ?>
<?php echo $row ?>
<?php else: ?>
Something else
<?php endif ?>
</tr>
<?php endforeach ?>
</table
The only downside with this is if you have a lotof mixed code it can make your document twice as long, which makes for more scrolling. Although if you have this much mixed code you may want to consider a templating engine.
唯一的缺点是如果你有很多混合代码,它会使你的文档长度增加一倍,这使得滚动更多。尽管如果您有这么多混合代码,您可能需要考虑使用模板引擎。
回答by Gajus
You should not be bothered about markup indentation in the production environment. Neither should you use Tidy or other HTML purifiers. There are valid use cases, e.g. when you allow HTML input (but consider using Markdowninstead), though these are rare.
您不应该为生产环境中的标记缩进而烦恼。您也不应该使用 Tidy 或其他 HTML 净化器。有一些有效的用例,例如当您允许 HTML 输入时(但请考虑使用Markdown),尽管这些情况很少见。
Most often HTML beautifiers-filters are abused to hide the underlying issues with the code. Don't. Correct your markup manually.
大多数情况下,HTML 美化过滤器被滥用来隐藏代码的潜在问题。别。手动更正您的标记。
If you need to indent your code only in the development environment, you can use either of the above. However, beware that these libraries will attempt to fix your markup (that's their primary purpose; indentation is a by-product). I've written Regular Expression based indentation tool Dindent.
如果你只需要在开发环境中缩进你的代码,你可以使用上面的任何一个。但是,请注意这些库会尝试修复您的标记(这是它们的主要目的;缩进是副产品)。我写了基于正则表达式的缩进工具Dindent。
Dindent will convert markup like this:
Dindent 会像这样转换标记:
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
console.log('te> <st');
function () {
test; <!-- <a> -->
}
</script>
<div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div><table border="1" style="background-color: red;"><tr><td>A cell test!</td>
<td colspan="2" rowspan="2"><table border="1" style="background-color: green;"><tr> <td>Cell</td><td colspan="2" rowspan="2"></td></tr><tr>
<td><input><input><input></td></tr><tr><td>Cell</td><td>Cell</td><td>Ce
ll</td></tr></table></td></tr><tr><td>Test <span>Ce ll</span></td></tr><tr><td>Cell</td><td>Cell</td><td>Cell</td></tr></table></div></div>
</body>
</html>
To this:
对此:
<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
console.log('te> <st');
function () {
test; <!-- <a> -->
}
</script>
<div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<div>
<table border="1" style="background-color: red;">
<tr>
<td>A cell test!</td>
<td colspan="2" rowspan="2">
<table border="1" style="background-color: green;">
<tr>
<td>Cell</td>
<td colspan="2" rowspan="2"></td>
</tr>
<tr>
<td>
<input>
<input>
<input>
</td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Ce ll</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>Test <span>Ce ll</span></td>
</tr>
<tr>
<td>Cell</td>
<td>Cell</td>
<td>Cell</td>
</tr>
</table>
</div>
</div>
</body>
</html>
Dindent will not attempt to sanitise or otherwise interfere with your code beyond adding indentation. This is to make your development/debugging easier. Not for production.
除了添加缩进外,Dindent 不会尝试清理或以其他方式干扰您的代码。这是为了使您的开发/调试更容易。不用于生产。

