PHP:构建(X)HTML输出与DOM - 优缺点? [英] PHP: building (X)HTML output with DOM - pros and cons?

查看:90
本文介绍了PHP:构建(X)HTML输出与DOM - 优缺点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用一个新的网站,我决定使用DOMImplementation来构建完整的网站。



你觉得怎么样,足够快吗?我希望它比基本的打印方法要慢一些。我更喜欢DOM,因为它在构建最终输出时允许更大的灵活性,更不用说更无错误的XHTML。无论如何,我至少要有一些速度比较。



你能否推荐我任何(也许是完全明显的)想法,以保持PHP代码清洁(即保留HTML在最低级别)?

解决方案

是否足够快?当然,这完全取决于你的网站。对于一个简单的或原型的站点,这是很好的,但我肯定不想在复杂的高流量站点部署一个DOM模板解决方案。



您将无法通过这种方式生成HTML兼容的XHTML;您必须在 saveXML 之间选择,它生成纯粹的,非IE兼容的XHTML,以及 saveHTML HTML4。 CDATA元素,如 style script 也可能需要注意。



DOM调用对于内容创建也是非常冗长的,像在JavaScript中创建节点而没有帮助函数或框架的好处。我不知道与PHP模板相比,生成的代码将是可读的。



您可以通过使用更简单的方式使PHP模板更干净一个一致的标记树,处理PHP结构,如<?php if(condition){?> ... <?php}?> ; 好像它们是XML开始和结束标签,并且始终将它们以格局良好的样式进行嵌套和缩进。参见例如这个问题进行一些讨论。 p>

I just started out with a new site and I decided to use DOMImplementation for building the complete site.

What do you think about this, is it fast enough? I expect it to be quite a bit slower than the basic print method. I prefer the DOM one because it allows greater flexibility when building the final output, not to mention the more error-free XHTML. Anyway, I would like to have some speed comparision at least.

Could you recommend me any (perhaps completely obvious) ideas to keep the PHP code clean (i.e. keep the HTML on a minimum level)?

解决方案

Is it fast enough? That totally depends on your site, of course. For a trivial or prototype site it'd be fine, but I certainly wouldn't want to deploy a DOM-templating solution on a complex, high-traffic site.

You won't be able to generate HTML-compatible-XHTML this way; you'll have to choose between saveXML which produces pure, non-IE-compatible XHTML, and saveHTML, which creates HTML4. CDATA elements like style and script may also need some attention.

DOM calls are also very verbose for content creation, like creating nodes in JavaScript without the benefit of helper functions or frameworks. I'm not sure the resulting code is going to be that readable, in comparison to PHP templating.

You can make PHP templating ‘cleaner’ in a simpler way by using a consistent markup tree, treating PHP structures like <?php if (condition) { ?> ... <?php } ?> as if they were XML start and end tags, and always nesting and indenting them consistently in the ‘well-formed’ style. See eg. this question for some discussion.

这篇关于PHP:构建(X)HTML输出与DOM - 优缺点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆