PHP 与模板引擎
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/731743/
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
PHP vs template engine
提问by Ikke
I'm currently having a discussion about the choice between PHP as a template engine versus a template engine on top of PHP.
我目前正在讨论选择 PHP 作为模板引擎还是基于 PHP 的模板引擎。
What is your choice, and why?
你的选择是什么,为什么?
I say why use another template engine when PHP is a template engine itself.
我说当 PHP 本身就是一个模板引擎时,为什么要使用另一个模板引擎。
回答by Robert K
For template engines:
对于模板引擎:
- Added security for end-user customization. Themes in pure PHP have unconstrained ability to cause harm to a user and their installation. Thus a template engine removes that risk, if it is a good one.
- Ease of use for non-programmers, such as graphic artists or web designers.
- 为最终用户定制增加了安全性。纯 PHP 中的主题具有不受限制的能力,可以对用户及其安装造成伤害。因此,模板引擎消除了这种风险,如果它是一个好的风险。
- 易于非程序员使用,例如图形艺术家或网页设计师。
For plain-php:
对于普通 php:
- The speed of pure PHP cannot be matched by any template engine built atop it.
- The fullpower of PHP is available to the output, and not just an interpreted or filtered portion.
- 纯 PHP 的速度是任何建立在它之上的模板引擎都无法比拟的。
- PHP的全部功能可用于输出,而不仅仅是解释或过滤的部分。
I prefer PHP itself if at all possible. And most folks don't want to hack your software by making a custom theme, so it's easy to take a cursory read and investigate its security. That said, I am the "between guy" who does both templating and programming, and even some graphic arts; my skillset differs from a strict programmer and a strict artist/designer.
如果可能的话,我更喜欢 PHP 本身。大多数人不想通过制作自定义主题来破解您的软件,因此很容易粗略阅读并调查其安全性。就是说,我是做模板和编程,甚至一些图形艺术的“中间人”;我的技能与严格的程序员和严格的艺术家/设计师不同。
回答by Robert K
I found that when I introduced Smarty, it was fairly straight forward to get web designers to produce HTML with smarty variables. The folks on the programming team now concentrate on more back-end work, that is, the production of the content of the Smarty variables.
我发现当我引入 Smarty 时,让 Web 设计师使用 smarty 变量生成 HTML 是相当简单的。编程团队的人现在专注于更多的后端工作,即 Smarty 变量内容的制作。
This has shortened the development lifecycle, with work being able to be split between more people, and has ultimately led to better designs.
这缩短了开发生命周期,工作能够在更多人之间分配,并最终导致更好的设计。
回答by rogeriopvl
Well, it's just my opinion, but template engines suck. You have to first understand how the template engine is implemented and then learn how to use it. It seems just wasted time, because PHP alone does it best and offers much more flexibility.
好吧,这只是我的意见,但模板引擎很糟糕。您必须先了解模板引擎是如何实现的,然后再学习如何使用它。这似乎只是浪费时间,因为 PHP 本身就做得最好,并且提供了更多的灵活性。
回答by cgp
The following reasons apply:
以下原因适用:
- Separating your application into templates with an engine makes your application less vulnerable to halting code errors
- Using templates can give you greater flexibility in the future when refactoring because the namespace won't be directly built into the application
- Using templates encourages (forces) developers to keep business logic and code OUT of the presentation layer.
- Using templates it is easier to mock up datasets and pass them to a template engine and get a preview of what the site will look like with data
- 使用引擎将您的应用程序分离为模板可以使您的应用程序不易受到暂停代码错误的影响
- 使用模板可以在将来重构时为您提供更大的灵活性,因为命名空间不会直接构建到应用程序中
- 使用模板鼓励(强制)开发人员将业务逻辑和代码保留在表示层之外。
- 使用模板可以更轻松地模拟数据集并将它们传递给模板引擎,并预览网站的数据外观
回答by Mark Biek
Using a template engine can be helpful if you have a non-programmer doing the templates. In many cases, the simplified template language can be easier for a non-programmer to pick up than PHP itself.
如果您有非程序员在做模板,那么使用模板引擎会很有帮助。在许多情况下,简化的模板语言对于非程序员来说比 PHP 本身更容易上手。
That said, I find myself moving away from using templates when it's just me (or me and other developers).
也就是说,当只有我(或我和其他开发人员)时,我发现自己不再使用模板。
回答by Zyx
PHP is nota template engine, but a language that can be used to write templates, or template engines. A template engine is not just a language, but also the programming API that allows the scripts to locate, organize templates or assign the data from the script to them. Pure PHP offers you absolutely nothing - it is just a language. Instead, you should take such libraries, as Zend_View in Zend Framework to comparisons (basically, it works exactly in the same way, as Smarty, except that it uses PHP to write templates). You should ask whether you should use a template engine with PHP or something else as a template language.
PHP不是模板引擎,而是一种可用于编写模板或模板引擎的语言。模板引擎不仅是一种语言,而且还是允许脚本定位、组织模板或将脚本中的数据分配给它们的编程 API。纯 PHP 绝对不会为您提供任何东西 - 它只是一种语言。相反,您应该将诸如 Zend Framework 中的 Zend_View 之类的库用于比较(基本上,它的工作方式与 Smarty 完全相同,只是它使用 PHP 编写模板)。您应该询问是否应该使用带有 PHP 或其他东西的模板引擎作为模板语言。
When it comes to templating languages themselves, then well... ordinary loops and conditions are enough to write templates, but this "enough" does not mean that it is easy, comfortable, efficient or flexible. PHP does not offer anything special for template designers, but many "templating languages" (like Smarty) provide just a limited subset of PHP, so I'm not surprised that programmers choose PHP. At least they can write functions and use OOP which is too massive for this (in my opinion), but does work and really helps.
说到模板语言本身,那么好吧……普通的循环和条件足以编写模板,但是这个“足够”并不意味着它容易、舒适、高效或灵活。PHP 并没有为模板设计者提供任何特别的东西,但许多“模板语言”(如 Smarty)只提供了 PHP 的一个有限子集,所以我对程序员选择 PHP 并不感到惊讶。至少他们可以编写函数并使用 OOP,这对于这个来说太大了(在我看来),但确实有效并且确实有帮助。
The point is that custom templating languages are not limited with PHP drawbacks, but their designers unsually do not see it, claiming that "displaying variables and a loop is enough". The possible areas, where templating languages could be much more effective:
关键是自定义模板语言不受 PHP 缺点的限制,但它们的设计者通常没有看到它,声称“显示变量和循环就足够了”。模板语言可能更有效的可能领域:
- Form displaying and rendering (I haven't seen any framework with PHP as a templating language that provided an easy, flexible and generic system for customizing the form look).
- Understanding the HTML/XML document structure.
- Automatic XSS injection filters.
- Solving various common problems in the presentation layer (i.e. customizing the look of the pagination system, displaying the data in columns etc.)
- Template portability and trueseparation of the application logic and implementation details from the templates.
- 表单显示和呈现(我还没有看到任何使用 PHP 作为模板语言的框架,它为自定义表单外观提供了一个简单、灵活和通用的系统)。
- 了解 HTML/XML 文档结构。
- 自动 XSS 注入过滤器。
- 解决表现层的各种常见问题(即自定义分页系统的外观,按列显示数据等)
- 模板可移植性和应用逻辑和实现细节与模板的真正分离。
Examples of templating languages that follow this way are mentioned above PHPTAL and Open Power Template 2. Some similar ideas can be also found in TinyButStrong, but unfortunately this template engine is extremely slow.
PHPTAL 和 Open Power Template 2 上面提到了遵循这种方式的模板语言的例子。在 TinyButStrong 中也可以找到一些类似的想法,但不幸的是,这个模板引擎非常慢。
回答by Kornel
PHP as template engine will not complain when you mix up your HTML syntax. It will let you forget to close tags, mis-nest them, etc.
当您混淆 HTML 语法时,PHP 作为模板引擎不会抱怨。它会让您忘记关闭标签、错误嵌套它们等。
PHP's output is not escaped by default, so unless you remember to rigorously add htmlspecialchars()everywhere, your site will have HTML injection (XSS) vulnerabilities.
PHP 的输出默认不会被转义,所以除非你记得在htmlspecialchars()所有地方严格添加,否则你的站点将存在 HTML 注入 (XSS) 漏洞。
<p>Hello <?= $name ?></b>
<!-- Simple template full of errors -->
These problems are much worse when you try to generate XHTML properly. It's not that you can't do that with plain PHP - of course you can - but that requires more effort and diligence.
当您尝试正确生成 XHTML 时,这些问题会严重得多。并不是说你不能用普通的 PHP 做到这一点——当然你可以——但这需要更多的努力和勤奋。
回答by seanhodges
PHP is perfectly suitable for most tasks, but a templating engine can help a project to scale more easily.
PHP 非常适合大多数任务,但模板引擎可以帮助项目更轻松地扩展。
Off the shelf ones like Smartyor PHPTALare great if you do not have the time to roll your own (and do not require more than they offer). Also, you can fairly easily replace/modify them with your own implementation later on if you find you need something more specialised.
如果您没有时间自己动手(并且不需要比他们提供的更多),那么像Smarty或PHPTAL这样的现成产品会很棒。此外,如果您发现需要更专业的东西,您可以很容易地用您自己的实现替换/修改它们。
I've personally had a good experience with PHPTAL, primarily because it keeps out of your way and is simple.
我个人对 PHPTAL 有很好的体验,主要是因为它不会妨碍您并且很简单。
回答by Derek Organ
Savant is what you are looking for. Its a nice wrapper class that allows you to use the PHP operators in your templates instead of interpreting an new template language on top of PHP.
Savant 正是您要找的。它是一个很好的包装类,允许您在模板中使用 PHP 运算符,而不是在 PHP 之上解释新的模板语言。
Pros:
优点:
It makes sense not to add extra work to the system.
No learning curve for developers
If you everyone is disciplined then this is the way to go. (Savant)
不向系统添加额外的工作是有意义的。
开发人员没有学习曲线如果你们每个人都遵守纪律,那么这就是要走的路。(学者)
Cons:
缺点:
Although Savant encourages you to separate properly it doesn't force the developer to separate business logic from development code. I have a rule that should never be broken. You can only output variables, use conditions and loops in your templates. You must never allow a developer to create variables in the template. Unfortunately developers never seem to do this no matter how many times you tell them. So, using a engine like Smarty then becomes worth it because the developers are forced to completely keep business and design apart.
尽管 Savant 鼓励您正确分离,但它并不强制开发人员将业务逻辑与开发代码分离。我有一条永远不应该被打破的规则。您只能在模板中输出变量、使用条件和循环。您绝不能允许开发人员在模板中创建变量。不幸的是,无论您告诉他们多少次,开发人员似乎都不会这样做。因此,使用像 Smarty 这样的引擎变得值得,因为开发人员被迫将业务和设计完全分开。
If I'm doing a project for myself or one that doesn't have many developers I tend to use Savant. If its a project that is much bigger than just me then in the Architecture I'll go for something like Smarty.
如果我正在为自己做一个项目或者一个没有很多开发人员的项目,我倾向于使用 Savant。如果它是一个比我大得多的项目,那么在架构中,我会选择 Smarty 之类的项目。
Either way, the separation in the code is important weather you use Savant or Smarty. I'm sure there are other good options out there too.
无论哪种方式,代码中的分离都是您使用 Savant 或 Smarty 的重要天气。我相信还有其他不错的选择。
回答by Skrol29
The following article sum-up the different points of view about Template Engines for PHP.
以下文章总结了有关 PHP 模板引擎的不同观点。
Doing PHP templates of the third kindhttp://www.tinybutstrong.com/article_3rd_kind.html
做第三类 PHP 模板http://www.tinybutstrong.com/article_3rd_kind.html

