dongnuo3749 2011-02-03 18:47
浏览 77
已采纳

基于XML的模板引擎与Smarty lexer

There was a lot of excitement about Smarty 3 and its new lexer and how much more power it would give you as a template designer, but when it actually hit the shelves as it were, it was a real disappointment how slow it was. Compiling a template from scratch took well over a second in Smarty 3, whereas the same template in Smarty 2 would take about half a second. Not good.

But it did get me thinking, why do you need to implement a full-blown language parser in PHP when it already has modules like DOMDocument, SimpleXML and the like available to it?

Are there any template engines for PHP that are based on the XML extensions and/or DOMDocument? If so, what is the performance like? If not, then has anybody attempted to write one?

One drawback I can forsee is it would only really be useful for XML-based formats such as XHTML and RSS. For generating other outputs (Non-XML HTML, plain text, CSS, etc) it would potentially be quite problematic, though I'm sure you could get round it with CDATA blocks. Are there any other implications for using XML/DOM for template parsing that I've not considered?

  • 写回答

2条回答 默认 最新

  • doudou0111 2011-02-03 19:15
    关注

    On your point about Smarty, IIRC Smarty uses "compiled templates", so if the performance issue you mention is only in the "compile phase" is becomes a moot point - each template only compiled once, thereafter the template content is output from (much faster) cache.

    The problem with using an XML parser is that HTML isn't always well formed XML. Even if you use valid XHTML you can end up jumping through hoops to support HTML entities, and then you'll find corner cases like embedded Javascript, etc. (On a side note, IMO this is HTML5's biggest failure - it doesn't deprecate all that legacy SGML crap and insist on using well-formed XML. If the HTML committee had done this then future templating engines would be much easier to write using standard XML APIs.) I wrote an XML-based template engine some time ago, this uses the XMLReader API, but to make it work with HTML you have to add entries in to your Libxml system catalog. This works well enough but is a pain, most people would just give up and use something simpler.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上