为什么几乎所有的PHP框架都使用“

发布于 2024-08-22 03:18:03 字数 880 浏览 3 评论 0原文

  • 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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

轻许诺言 2024-08-29 03:18:03

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.

下雨或天晴 2024-08-29 03:18:03

我不知道我会调用 “超长语法。”

事实上,短标签并不总是在服务器上启用,而标准默认值通常是启用的。走那条路比较安全。

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.

负佳期 2024-08-29 03:18:03

像 Smarty 这样的模板引擎添加了一个不需要的额外处理层 - 它们大多是英国媒体报道软件。他们通常会添加太多额外的处理,相当于语法糖。当完整的 PHP 标签可用时,使用模板引擎就像戴上了镣铐——它变成了另一种语言,有自己的怪癖需要学习,以便用常规 PHP 完成同样的事情。

根据我的经验,我很少看到非程序员完全或轻松地利用模板引擎。以这两个例子为例:

Smarty:

<select>
{foreach from=$k item=v}
 <option value="{$v.value|escape:'html'}">{$v.label|escape:'html'}</option>
{/foreach}
</select>

PHP:

<select>
<?php foreach ($k as $v) { ?>
 <option value="<?php echo htmlentities($v['value']); ?>"><?php echo htmlentities($v['label']); ?></option>
<?php } ?>
</select>

现在,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:

<select>
{foreach from=$k item=v}
 <option value="{$v.value|escape:'html'}">{$v.label|escape:'html'}</option>
{/foreach}
</select>

PHP:

<select>
<?php foreach ($k as $v) { ?>
 <option value="<?php echo htmlentities($v['value']); ?>"><?php echo htmlentities($v['label']); ?></option>
<?php } ?>
</select>

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.

悟红尘 2024-08-29 03:18:03

以下是 Symfony 框架的 Fabien Potencier 关于模板引擎的说法:

为什么人们仍然认为 PHP 是
模板引擎?果然是PHP
作为模板开始了它的生活
语言,但它并没有像
近几年的一件事。如果你认为
PHP仍然是一种模板语言,可以
你只给我一个最近的变化
PHP 语言将 PHP 增强为
模板语言?我想不出
一个。

他还描述了他在模板语言中寻找的功能:

  • 简洁
  • 面向模板的语法
  • 可重用性
  • 安全
  • 沙盒模式

以及一些使他最喜欢的模板语言 Twig 的功能脱颖而出:

  • 本机模板继承(模板编译为类);
  • 可靠的自动转义(没有相关的运行时开销
    一切都在期间完成
    编译);
  • 非常安全的沙盒模式(将标签、过滤器和方法列入白名单)
    可以在模板中使用);
  • 出色的可扩展性:您可以通过以下方式覆盖所有内容,甚至是核心功能
    将您自己的标签和过滤器捆绑为
    扩展;但你也可以
    操纵 AST(抽象语法
    编译前的树)。经过
    利用这种可能性,您可以
    甚至创建您自己的 DSL(域
    特定语言),针对您的
    应用程序。

在文章的评论中,他表示,“它可能会成为 Symfony 2 的一部分。但我首先需要一些社区反馈。”

阅读全文,让他的整个论点支持模板系统。

Here's what Fabien Potencier of the Symfony framework has to say about templating engines:

Why do people still think PHP is a
templating engine? Sure enough, PHP
started its life as a template
language, but it did not evolve like
one in the recent years. If you think
PHP is still a template language, can
you give me just one recent change in
the PHP language which enhanced PHP as
a template language? I cannot think of
one.

He also describes the features that he looks for in a templating language:

  • Concision
  • Template oriented syntax
  • Reusability
  • Security
  • Sandbox mode

As well as some of the features that make his favorite templating language Twig stand out:

  • Native template inheritance (templates are compiled as classes);
  • Solid automatic auto-escaping (with no associated runtime overhead as
    everything is done during
    compilation);
  • Very secure sandbox mode (white-list the tags, filters, and methods that
    can be used in templates);
  • Great extensibility: you override everything, even the core features, by
    bundling your own tags and filters as
    an extension; but you can also
    manipulate the AST (Abstract Syntax
    Tree) before compilation. By
    leveraging this possibilities, you can
    even create your own DSL (Domain
    Specific Language), targeted at your
    application.

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.

残月升风 2024-08-29 03:18:03

有一些原因:

  • 出于安全考虑,它们将在未来版本的 php 中被弃用。
  • 有些主机禁用它们。

更多:

PHP 短标签是否可接受使用?

不推荐它们,因为它是
PITA 如果您需要移动您的
将代码发送到不在服务器上的服务器
支持(并且您无法启用它)。
正如你所说,很多共享主机都这样做
支持短标签,但“很多”并不是全部
其中。如果您想分享您的
脚本,最好使用完整的
语法。

我同意

我不以可读性为理由
全部。最认真的开发商有
语法高亮选项
可供他们使用。

更多

http://terrychay.com/article/short_open_tag.shtml

There are some reasons:

  • They are to be deprecated from future versions of php due to security concerns.
  • Some hosts disable them.

More:

Are PHP short tags acceptable to use?

They're not recommended because it's a
PITA if you ever have to move your
code to a server where it's not
supported (and you can't enable it).
As you say, lots of shared hosts do
support shorttags but "lots" isn't all
of them. If you want to share your
scripts, it's best to use the full
syntax.

I agree that

I don't buy readability as a reason at
all. Most serious developers have the
option of syntax highlighting
available to them.

More

http://terrychay.com/article/short_open_tag.shtml

鼻尖触碰 2024-08-29 03:18:03

短开放标签不会被弃用,并且在 PHP6 中也不会被删除。

链接的文章还包含大量有关该主题的有用信息。

引用Rasmus Lerdorf(第三个链接):

我看到的大多数论点基本上都在说 是邪恶的,它甚至不应该存在,但这不是当前的问题。它确实存在,而且我们不会删除它,因此这里唯一真正的论点是能够动态启用或禁用这些标签的代码引入的 WTF 因素。这是我见过的唯一有效的论点。 PHP 代码是否可以使用 xmllint 进行验证以及 是否是有效的 xml(显然不是)完全不是重点。我们都知道,当您使用 时,您就不符合 XML 标准。对于绝大多数人来说这没关系。 [...]

我的观点是人们想要模板。尽管我非常讨厌这个概念,并且一直对此非常直言不讳,但人们想要更简单的模板标签。他们甚至会在每个请求上解析文件并生成 PHP 代码,以便使用 {blah} 而不是 。事实上,人们愿意为语法糖而承受一个数量级的性能损失,这让我感到困惑,但只要看看现有的所有模板系统就知道了。是的,我知道使用模板还有其他原因,例如限制不受信任的模板编写者的功能集等。但是您会惊讶地发现有多少人只是想减少输入并让他们的标签更漂亮。在我看来,让这些人切换到 是性能和理智的胜利。是的,这不是完全的胜利,但它仍然是胜利。


就我个人而言,我尽量避免使用模板系统,因为我发现常规 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):

Most of the arguments I have seen are basically saying <? is evil and it shouldn't even exist, but that isn't the current question. It does exist, and we aren't removing it, so the only real argument here is the WTF factor introduced by code that is able to enabled or disable these tags on the fly. That's the one and only valid argument I have seen. Whether or not PHP code can be validated with xmllint and whether or not <? is valid xml, which it obviously isn't, is completely beside the point. We all know that when you use <? you are not XML-compliant. And for the vast majority that's ok. […]

My view is that people want templating. As much as I hate the concept, and have always been very vocal about that, people want simpler templating tags. They will even go as far as parsing files and generating PHP code on every single request in order to use {blah} instead of <?php blah() ?>. The fact that people are willing to take an order of magnitude performance hit for syntactic sugar is baffling to me, but just look at all the templating systems out there. And yes, I know there are other reasons to use templating such as restricting the feature set for untrusted template writers, etc. but you'd be surprised how many people just want to type less and have their tags be prettier. Getting these folks to switch to <?blah()?> is a win for performance and sanity in my book. Yes, it isn't a full victory, but it is still a win.


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.

離人涙 2024-08-29 03:18:03

因为他们不想包含像 Smarty 这样庞大的库来使他们的模板短一些字符。

Because they don't want to include a library as huge as Smarty to make their templates a few characters shorter.

贱贱哒 2024-08-29 03:18:03

除了其他答案之外,还有一些事情使 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文