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:
我也经常思考这个问题,但后来我意识到,谁在乎 HTML 输出是什么样子呢? 无论如何,您的用户不应该查看您的 HTML。 它适合您阅读,也可能供其他几个开发人员阅读。 保持源代码尽可能干净,并忘记输出的样子。
如果您需要调试输出,请使用 Chrome 开发者工具、Firebug 甚至 F12 工具。
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 YOU to read, and maybe a couple other developers. Keep the source code as clean as possible and forget about what the output looks like.
If you need to debug the output, use Chrome Developer Tools, Firebug, or even F12 Tools.
I generally put opening php 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.
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's answer.
Better still in my opinion: Don't mix HTML and php, use a template engine instead.
The only downside with this is if you have a lot of 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.
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 Markdown instead), though these are rare.
Most often HTML beautifiers-filters are abused to hide the underlying issues with the code. Don't. Correct your markup manually.
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 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.
发布评论
评论(6)
PHP 和 HTML 都应该缩进,以便它们在源形式中本身是正确的,无论彼此如何以及输出形式如何:
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:
我也经常思考这个问题,但后来我意识到,谁在乎 HTML 输出是什么样子呢? 无论如何,您的用户不应该查看您的 HTML。 它适合您阅读,也可能供其他几个开发人员阅读。 保持源代码尽可能干净,并忘记输出的样子。
如果您需要调试输出,请使用 Chrome 开发者工具、Firebug 甚至 F12 工具。
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 YOU to read, and maybe a couple other developers. Keep the source code as clean as possible and forget about what the output looks like.
If you need to debug the output, use Chrome Developer Tools, Firebug, or even F12 Tools.
我通常将 开始 php 标签放在行的开头,但缩进标签内的所有内容以匹配 html 格式。 然而,对于简单的 echo 语句,我不会这样做,因为我使用短开标签。 我认为浏览文件以查找所有声明时会更简单。
I generally put opening php 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.
您也可以随时使用一些空格来提高可读性。 建立在混乱的缩进之上:
唯一的缺点是,如果您有很多混合代码,它可能会使您的文档长度增加两倍,从而需要更多的滚动。 不过,如果您有这么多混合代码,您可能需要考虑模板引擎。
You can always use a bit of whitespace too to help readability. Building on chaos' indentation:
The only downside with this is if you have a lot of 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.
您不应该担心生产环境中的标记缩进。 您也不应该使用 Tidy 或其他 HTML 净化器。 有一些有效的用例,例如当您允许 HTML 输入时(但请考虑使用 Markdown 代替)这些都是罕见的。
大多数情况下,HTML 美化器过滤器被滥用来隐藏代码的潜在问题。 不。 手动更正您的标记。
如果您只需要在开发环境中缩进代码,则可以使用上述任一方法。 但是,请注意这些库将尝试修复您的标记(这是它们的主要目的;缩进是副产品)。 我编写了基于正则表达式的缩进工具Dindent。
Dindent 将像这样转换标记:
为此:
除了添加缩进之外,Dindent 不会尝试清理或以其他方式干扰您的代码。 这是为了让您的开发/调试更容易。 不用于生产。
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 Markdown instead), though these are rare.
Most often HTML beautifiers-filters are abused to hide the underlying issues with the code. Don't. Correct your markup manually.
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 will convert markup like this:
To this:
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.