为什么几乎所有的PHP框架都使用“”
- PHP 短标签
已被弃用一段时间。
- 几乎所有 PHP 框架 都使用长格式
(例如,交响乐, Yii, Kohana)
- Smarty 是一个著名的 PHP 模板引擎,支持较短的形式
{$var}
- 模板引擎(如 Smarty)对于网页设计师来说更容易
- 编辑模板时会显示
{$var}
,而不是不显示任何内容(因为<...>
) - 更短的语法(更少的打字,尤其是当
<>
在某些键盘布局上的同一个键上时) - 模板经过预编译和缓存,提供几乎相同的性能
- 编辑模板时会显示
所有这些点都让我想知道为什么所有框架似乎都使用超长的 PHP 语法?不使用像 Smarty 这样的模板引擎有什么优点(除了很小的开销)?
- The PHP short tag
<?= $var ?>
has been deprecated for a while. - Almost all PHP frameworks use the long form
<?php echo $var ?>
(e.g., symphony, Yii, Kohana) - Smarty is a famous PHP template engine which supports a shorter form
{$var}
- Template engines (like Smarty) are easier for web designer
- Editing a template shows
{$var}
instead of showing nothing (because of<..>
) - Shorter syntax (less typing especially when
<>
are on the same key on some keyboard layout) - The templates are pre-compiled and cached giving nearly the same performances
- Editing a template shows
All those points make me wonder, why do all framework seem to use the super long PHP syntax? Is there a strong point of not using a template engine like Smarty (except the small overhead)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
PHP 的特点是它已经是一种模板语言。
Smarty 虽然很好,但会增加开销。如果你没有充分的理由使用它,那你为什么要使用它呢?使用后端框架的人都是已经熟悉 PHP 的开发人员,因此没有理由让他们使用具有新语法的模板引擎来学习 PHP。
大多数框架都足够灵活,添加模板引擎并不需要太多工作。如果构建的框架强迫您使用 Smarty,那么它将不太受欢迎,因为框架本身的灵活性会较差。
关于“长语法”,没有任何框架会因为安全问题而对已弃用的语法表示不满。框架的用户是否想使用它可以由他们决定(现在没有人应该这样做),但是围绕短标签构建核心框架会降低它的可移植性。
The thing about PHP is that it already is a templating language.
Smarty, as nice as it is, is added overhead. If you don't have a good reason to use it, then why would you? People who use backend frameworks are developers who are already familiar with PHP, so there's no reason to make them use a templating engine with a new syntax to learn on top of it.
Most frameworks are flexible enough that adding a template engine doesn't take much work. If a framework was built that forced you to use Smarty, then it's going to be less popular, because the framework itself would be less flexible.
In regards to the "long syntax", no framework is going to hang their hat on a deprecated syntax with security issues. It can be left up to the user of the framework if they want to use it or not (which no one should be these days), but building a core framework around short tags makes it less portable.
我不知道我会调用
“超长语法。”
事实上,短标签并不总是在服务器上启用,而标准默认值通常是启用的。走那条路比较安全。
I don't know that I would call
<?php print $foo; ?>
"super long syntax."The fact is that short-tags aren't always enabled on servers, whereas the standard default typically is. It's safer going that route.
像 Smarty 这样的模板引擎添加了一个不需要的额外处理层 - 它们大多是英国媒体报道软件。他们通常会添加太多额外的处理,相当于语法糖。当完整的 PHP 标签可用时,使用模板引擎就像戴上了镣铐——它变成了另一种语言,有自己的怪癖需要学习,以便用常规 PHP 完成同样的事情。
根据我的经验,我很少看到非程序员完全或轻松地利用模板引擎。以这两个例子为例:
Smarty:
PHP:
现在,Smarty 语法可能稍微干净一些 - 但老实说,除了程序员之外,还有人能够轻松地使用任一代码集吗?模板引擎添加了额外的处理/逻辑层,但没有提供任何重大好处。
Template engines like Smarty add an additional layer of processing that is not needed - they are mostly bloatware. They usually add too much additional processing for what amounts to syntactic sugar. Using a template engine when full PHP tags are available is like wearing shackles - it becomes another language with it's own quirks to learn in order to accomplish the same thing with regular PHP.
In my experience I've rarely seen a non-programmer fully or easily utilize a template engine. Take these two instances:
Smarty:
PHP:
Now, the Smarty syntax may be slightly cleaner - but honestly, is anyone except a programmer going to be able work with either code set comfortably? Template engines add an extra layer of processing/logic without offering any major benefits.
以下是 Symfony 框架的 Fabien Potencier 关于模板引擎的说法:
他还描述了他在模板语言中寻找的功能:
以及一些使他最喜欢的模板语言 Twig 的功能脱颖而出:
在文章的评论中,他表示,“它可能会成为 Symfony 2 的一部分。但我首先需要一些社区反馈。”
阅读全文,让他的整个论点支持模板系统。
Here's what Fabien Potencier of the Symfony framework has to say about templating engines:
He also describes the features that he looks for in a templating language:
As well as some of the features that make his favorite templating language Twig stand out:
In the comments of the article, he says that, "It will probably be part of Symfony 2. But I first need some community feedback."
Read the full article to get his whole argument in favor of templating systems.
有一些原因:
更多:
PHP 短标签是否可接受使用?
更多
http://terrychay.com/article/short_open_tag.shtml
There are some reasons:
More:
Are PHP short tags acceptable to use?
More
http://terrychay.com/article/short_open_tag.shtml
短开放标签不会被弃用,并且在 PHP6 中也不会被删除。
链接的文章还包含大量有关该主题的有用信息。
引用Rasmus Lerdorf(第三个链接):
就我个人而言,我尽量避免使用模板系统,因为我发现常规 PHP 语法更容易用作模板语言,它重塑了 PHP 开箱即用的功能。添加 ViewHelpers 后,任何设计人员在使用常规语法时都不会有太多麻烦。事实上,我一直认为,模板引擎(如 Smarty)对于网页设计师来说更容易 相当贬低。冗长的 PHP 语法可能不美观,但任何半脑人都可以学会它。
Short open tags are not deprecated and they will also not be removed in PHP6.
The linked articles also contain lots of useful information about the topic.
Quoting Rasmus Lerdorf (3rd link):
Personally, I try to avoid templating systems as I find regular PHP syntax much easier to use as a templating language that reinvents what PHP can do out of the box. With the addition of ViewHelpers, any designer shouldn't have too much trouble using regular syntax. Actually, I always found the argument, Template engines (like Smarty) are easier for web designer pretty belittleing. The verbose PHP syntax might not be appealing aesthetically, but any half-brain can learn it.
因为他们不想包含像 Smarty 这样庞大的库来使他们的模板短一些字符。
Because they don't want to include a library as huge as Smarty to make their templates a few characters shorter.
除了其他答案之外,还有一些事情使 Smarty(和类似的模板引擎)出现问题。
首先,如果要在模板中放入 javascript,则需要对模板中的某些字符进行转义。如果你的 javascript 是由 PHP 动态创建的,情况会变得更糟;该代码的可读性急剧下降。
第二个也是最重要的一点是,当您在一个像样的 OO 框架中工作时,Smarty 会严重降低代码的功能。当使用 PHP 作为模板引擎时,您可以在模板中使用 $this 来调用解析模板的控制器中的方法。不仅如此,您还可以访问控制器继承的所有方法。
使用 smarty,您将失去整个功能,因为 $this 不再引用您的控制器。本质上,您只能访问 Smarty 的有限功能,而不是从模板访问整个框架。
In addition to the other answers there are a couple of things that make Smarty (and similar template engines) problematic.
The first is that you need to escape some characters in your template if you're going to put javascript in it. If your javascript is dynamically created by PHP, it gets even worse; the readability of that code goes down drastically.
The second and most important is that when you're working in a decent OO framework Smarty will severely decrease the functionality of your code. When using PHP as your template engine you can use $this within your template to call methods in the controller that's parsing the template. Not only that, but you get access to all the methods that controller has inherited.
With smarty you lose this entire functionality because $this no longer refers to your controller. In essence, instead of accessing your entire framework from your template, you only have access to Smarty's limited functionality.