何时使用PHP模板引擎 [英] When to use PHP template engines

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

问题描述

我正在用php建立一个网页很多的网站,我们是一个由9个人组成的团队.因此,只想探讨一下何时应该使用PHP模板引擎以及何时不应该使用PHP模板引擎.因此,我对使用PHP模板引擎的利弊感兴趣,因此我可以决定是否在我的情况下使用它.

I am building a website in php with lot of pages and we are a team of 9 working on it. So just want to explore that when should we use PHP template engines and when we shouldn't. So I'm interested in pros and cons of using PHP Template engines so I can make a decision whether to use it in my case or not.

推荐答案

PHP是一种模板语言.

到目前为止,根据我在各种模板系统上的经验,得出的结论非常简单:不使用.

相反,应编写PHP,因为它应该被编写!除了echofor/foreach,切勿在.html内执行任何操作.如果您基于MVC之类的设计模式编写代码,这将变得更加明显:仅在内部使用echoforeach并说明前端,它们切勿与<?php?>内部的代码搞混.

Instead, write PHP as it should be written! Never do anything inside a .html besides echo and for/foreach. If you write the code based on a design pattern like MVC this becomes even more obvious: just echo and foreach inside and explain the frontends they should never mess with the code inside <?php and ?>.

从那时起,它对我一直有效.对于我来说,向他们解释Smarty通常比解释从不搞乱php更加困难.

It worked for me ever since. It usually was harder for me to explain them Smarty than to explain to never mess with php.

模板系统也增加了服务器的重量(有时只是一点点,有时您可能会感觉到).乍一看似乎过分优化,但我宁愿保持它尽可能简单.

Template systems also add weight to your server (sometimes is just a bit, sometimes you may feel it). It may seem over-optimization at first, but I prefer to keep it as simple as it can be.

注意:

Smarty是 HARDER ,可以在.html文件中发现,因为我知道的唯一Smarty语法突出显示器是NetBeans插件,并且它是beta版本.另一方面,PHP在任何适当的编辑器中都突出显示了其语法.前端也更容易发现而不被弄乱.

Smarty, for example, is HARDER to spot in a .html file because the only Smarty syntax highlighter I know is a NetBeans plugin and it's pretty beta. PHP, on the other hand, has it syntax highlighted in any decent editor. Also easier for the frontends to spot and not mess with.

  • 增加服务器负载(更轻或更重,没关系-您可以看到它)
  • 引发不良做法(逻辑封装在模板语言语法中)
  • 没有突出显示模板语言语法的语法-很难发现(对于编码器和前端)
  • 花时间学习并将其教授到前端
  • 更难向前端团队解释(我已经教过几次基本的PHP到前端-虽然还有更多人已经能够编写自己的入门级" PHP;但我从不教过前端Smarty,以便他们可以执行{$var}以外的其他功能)
  • 使您避免出现 REAL问题:逻辑和演示文稿分离
  • 为您的项目增添分量
  • Increases server load (lighter or heavier, doesn't matter - you can see it)
  • Induces bad practice (logic wrapped inside template language syntax)
  • No syntax highlighting for template languages' syntax - harder to spot (for coder and frontend)
  • Time spent learning it and teaching it to the frontends
  • Harder to explain to the frontend team (I've taught basic PHP to frontends several times - while many more were already able to write their own 'beginner-level' PHP; I have never taught a frontend Smarty so they can do something other than {$var} )
  • Makes you avoid the REAL problem: logic and presentation separation!
  • Adds extra weight to your project
  • 无聊(可能是唯一有效的论点)
  • 逻辑和表示分离(我建议 MVC 用于此任务及其优点,它为其他开发领域提供了方便:易于维护,抽象数据库等)
  • 强制自己仅在视图中编写echo并进行迭代以进行回显:foreachfor应该满足99%的迭代需求;您还可以使用whiledo while
  • Logic and presentation separation (I'd suggest MVC for this task and for the pros it provides for other fields of development: easier maintenance, abstract database and so on)
  • Force yourself to write in the view only echo and iteration for echoing: foreach and for should accomplish 99% of the iteration needs; you can also use while and do while

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

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