纯PHP / HTML视图VS模板引擎视图 [英] Pure PHP/HTML views VS template engines views

查看:164
本文介绍了纯PHP / HTML视图VS模板引擎视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道哪种方法更快,在HTML文件中使用纯PHP或使用Smarty,Twig等模板引擎...
我特别想知道的是接下来的是:解析速度更快,例如Smarty缓存比使用纯PHP更快吗?
哪个模板引擎是最快的?我将重写简单的应用程序,其中速度是第一位的。

取决于是所有你的答案问题。



什么是更快?执行时间处理时间?开发时间?保养?内存开销?他们的混合物?模板引擎通常用于某些性能(速度,内存)的交易以获得更好的开发和维护。



如果您在谈论纯动态模板(意思是:请求)PHP将超出任何模板引擎。这真是一个拙劣的人。如果考虑到缓存,像Smarty这样的模板引擎可能会有所帮助。尽管如此,缓存是你无法在普通PHP中实现的东西。对于Smarty,它只是为你完成的(并且比你想象的要复杂得多)。



如果你使用的是一个框架,比如Symfony,它可能是因为Twig和Symfony紧密集成,所以明智地使用Twig。当然你可以使用Smarty或普通的PHP。这里的问题是:是否可行?

当从数据源构建网站时,如数据库或远程API,缓存是有意义的。你真正保存的东西(从某种意义上来说)是数据库调用,密集计算等等。检查是否有任何时间密集型功能运行来构建你的站点。如果是这样,请使用缓存(如果可以的话)。



了解开发/维护/便利/性能权衡,我会(总是)推荐使用模板引擎。作为一名Smarty开发人员,我当然会建议使用Smarty。除非你使用Symfony,否则你可能会更适合使用Twig。或者其他一些以其他模板引擎为特色的框架。



请忽略 Smarty vs. Twig ,因为他们只是比较了一个非常有限的引擎视图。不要相信你没有伪装的基准™。

一般来说,Smarty 3.1比Twig快一点。 Twig在运行时(正在执行模板的时候)正在做很多Smarty在编译时(即模板准备执行时)所做的工作。 Twig在这里并不是真的在放慢速度。 Twig需要在运行时通过设计来做某些事情。他们的表现有点方便(例如,使用相同的符号访问数组和对象)。


I would like to know which approach is faster, using the pure PHP in the HTML files or using a template engines like Smarty,Twig, ... What I would particularly like to know is next: which is parsed faster, is the Smarty cache for example faster than using pure PHP? Which of the template engines is the fastest? I'm about to rewrite simple application where speed is on the first place.

解决方案

"Depends" is the answer to all your questions.

What is "faster"? Execution time? Development time? Maintenance? Memory overhead? A mixture of them? A template engine is usually trading in some performance (speed, memory) for better development and maintenance.

If you are talking about purely dynamic templating (meaning: template evaluated on every request) PHP will outrun any template engine. This is a nobrainer, really. If you're taking caching into account, a template engine like Smarty may help. Caching is nothing you couldn't implement yourself in plain PHP, though. With Smarty it's just been done for you (and on a far more sophisticated level than you possibly would).

If you are using a framework, say Symfony, it might be wise to use Twig, as Twig and Symfony are tightly integrated. Sure you can use Smarty or plain PHP. The question here is: is it practicable?

Caching makes sense when building sites from datasources like a database or remote APIs. What you are really saving (in a sense of reducing) here are database calls, intensive calculations, etc. Check if you have any time-intensive functions running to build your site. If so, use caching (if you can).

Knowing development/maintenance/convenience/performance trade-offs, I would (always) recommend using a template engine. Being a Smarty developer, I'll, of course, suggest using Smarty. That is unless you're using Symfony, then you might be better of with Twig. Or some other framework featuring some other template engine.

Please ignore posts like Smarty vs. Twig, as they only compare a very limited view of the engines. Don't trust benchmarks you haven't faked yourself™.

In general, though, Smarty 3.1 is a bit faster than Twig. Twig is doing a lot of stuff at runtime (being the time when a template is executed) that Smarty does on compile time (being the time when a template is prepared for execution). Twig is not really pissing away speed here. Twig needs to do certain stuff at runtime by design. They traded a bit of performance for a bit of "convenienve" (Accessing arrays and objects with the same notation, for example).

这篇关于纯PHP / HTML视图VS模板引擎视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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