如何在 PHP 中更好地使用 Smarty?

发布于 2024-07-19 22:21:18 字数 477 浏览 4 评论 0原文

我发现在 PHP 中使用 Smarty,有时需要花费额外的时间

1) 使用与 PHP 本身完全不同的语法
2)需要检查小案例,因为文档没有提供更详细的细节,例如“escape”

http://www.smarty.net/manual/en/language.modifier.escape.php

它没有说转义:“quotes”仅适用于双引号或单引号同样,因此您需要编写代码来测试它。 同样对于转义的情况:“javascript” - 无法准确说出它是如何转义的。

3)对于复杂的事情,需要编写辅助函数或修饰符,因此需要创建新文件并最终在 PHP 中再次执行。

顺便问一下,使用 Smarty 是否比单独使用 PHP 提供更好的速度? 谢谢。

I found that using Smarty with PHP, sometimes extra time will need to be used for

1) using quite different syntax than PHP itself
2) need to check small cases, because documentation doesn't give finer details, such as for "escape"

http://www.smarty.net/manual/en/language.modifier.escape.php

it doesn't say escape:"quotes" is for double quotes only or for single quotes as well, so you need to write code to test it. Also for the case of escape:"javascript" -- can't tell exactly what and how it is escaped.

3) for something complicated, need to write helper functions or modifiers, so it needs a creation of new files and ending up doing it in PHP again.

by the way, does using Smarty provide a good speed up over use PHP alone? thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(11

破晓 2024-07-26 22:21:18

首先,PHP 是一种模板语言。 当您谈论为基于 PHP 的 Web 应用程序使用模板系统时,请记住这一点。

我听过的关于使用任何模板引擎的唯一“真实”论点是,它们为模板操作提供了一种更简单的语言,如果您的模板设计者不了解 PHP 并且您不信任他们,那么这会很方便学习明智地使用 PHP。

关于这些论点,我认为,如果您的模板设计人员没有能力学习足够的 PHP 来进行模板设计,您可能应该考虑寻找新的模板设计人员。 此外,PHP 本身为控制语句提供了不同的语法,您可以在模板中使用与在代码中使用的语法。 例如:

<? foreach($array as $key => $val): ?>
    <?= $val ?>
<? endforeach; ?>

VS:

<?php
    foreach($array as $key => $val) {
        echo $val;
    }

?>

就我个人而言,我相信 PHP 中出现了模板引擎,因为:

  1. 这就是其他语言的做法
  2. 更好的 PHP 程序员意识到他们需要一种方法来强制表示和应用程序逻辑之间的分离,而模板是实现此目的的一种简单方法。

第一个原因有点愚蠢。 第二个原因可以通过一点自我控制甚至对应用程序中分层的必要性的基本了解来克服。 MVC 设计模式是解决这个问题的一种方法。 就行使一些自我控制而言,我的规则是只使用必要的循环和 if 语句以及过滤、转义、格式化屏幕输出的函数。

在广泛使用 Smarty 后,我可以诚实地说,它总是给我带来比它提供的解决方案更多的障碍。 如果说有什么不同的话,那就是改用基于 PHP 的模板实际上减少了模板和代码的开发时间。

First, PHP is a templating language. Keep that in mind when you talk about using a template system for your PHP-based web applications.

The only 'real' argument that I've ever heard for using ANY templating engine was that they provide a simpler language for template manipulation which can be handy if you have template designers who don't know PHP and whom you don't trust to learn to use PHP judiciously.

Regarding these arguments, I would argue that if your template designers are not competent to learn enough PHP for template design, you should probably consider finding new template designers. Additionally, PHP itself provides a different syntax for control statements that you might use in a template versus in code. For example:

<? foreach($array as $key => $val): ?>
    <?= $val ?>
<? endforeach; ?>

VS:

<?php
    foreach($array as $key => $val) {
        echo $val;
    }

?>

Personally, I believe that templating engines arose in PHP because:

  1. That's way that other languages do it
  2. Better PHP programmers realized that they needed a way to enforce separation between presentation and application logic and templates were an easy way to do this.

The first reason is just kinda silly. The second reason can be overcome with a little self-control and even a rudimentary understanding of the necessity of separating layers in an application. The MVC design pattern is one way of approaching this problem. As far as exercising some self-control, my rule is that only necessary loops and if statements get used as well as functions that filter, escape, format the output for the screen.

Having used Smarty extensively, I can honestly say that it always presented me with more hurdles to get over than it did solutions. If anything, switching to PHP-based templates is what has actually decreased development time for both templates and code.

云归处 2024-07-26 22:21:18

我不喜欢模板引擎。 我发现它们对于 PHP 来说非常有损耗且资源密集。

对于 MediaWiki,在 1.6.x 版本左右,我们默认放弃使用 Smarty,只使用 PHP 的内置模板,性能有了很大的提高。

我发现人们想要使用模板系统做的大多数事情(添加链接、更改颜色、删除页面文本或部分)最好使用简单的事件挂钩系统来完成。

Laconica,开放的微博平台,默认情况下不做任何模板。 我们为那些热衷于模板的人们提供了一个插件。

I don't like templating engines. I find them very lossy and resource-intensive for PHP.

With MediaWiki, around version 1.6.x we backed off using Smarty by default and just use PHP's built-in templating, with a great improvement in performance.

I've found that most of what people want to do with a templating system (add links, change colors, remove text or sections of the page) are better done with a simple system of event hooks.

Laconica, the open microblogging platform, doesn't do any templating by default. We have a plugin for people who are crazy for templating.

神经大条 2024-07-26 22:21:18

我喜欢模板引擎并认为应该使用它们,但在 Smarty 的特殊情况下,我认为这是浪费时间,因为它相对于 PHP 作为模板语言来说并没有重大改进:

  • 新语法仍然基于特殊标签的旧概念插入到文档中的随机位置。
  • 由于 Smarty 不理解 HTML 的语法/结构,因此它无法帮助您创建有效/格式良好的 HTML。 Smarty 的标签违反了 HTML 的语法,因此一旦添加它们,其他标准工具也无法帮助您。
  • Smarty 的输出,就像在 PHP 中一样,默认情况下是不安全的(未转义的),您必须记住在 HTML 中输出数据的所有位置添加 |escape

我爱上了一个特定的 PHP 模板引擎,它解决了所有这些问题:PHPTAL

它仍然是您必须学习的新东西,并且它是您的应用程序的依赖项,但我认为解决了 XSS 和格式不正确的问题就值得了。

PHPTAL 就像 Smarty 一样,一次编译为 PHP 并缓存,因此性能与原始 PHP 相当。

I like template engines and think they should be used, but in the particular case of Smarty, I think it's waste of time, because it's not a significant improvement over PHP as templating language:

  • The new syntax is still based around old concept of special tags inserted in random places in the document.
  • Because Smarty does not understand HTML's syntax/structure, it cannot not help you to create valid/well-formed HTML. Smarty's tags violate HTML's syntax, so once you add them, other standard tools can't help you either.
  • Smarty's output, just like in PHP, is insecure (unescaped) by default and you have to remember to add |escape everywhere where you output data in HTML.

There's one particular PHP template engine that I've fallen in love with, which fixes all those problems: PHPTAL.

It's still something new you have to learn, and it's a depenency for your application, but I think having XSS and ill-formedness problems solved makes it worth the trouble.

PHPTAL just like Smarty is compiled once to PHP and cached, so performance is comparable to raw PHP.

可爱暴击 2024-07-26 22:21:18

Smarty 无疑是最好的模板引擎之一。 根据我的经验,我们强烈建议人们在使用 PHP 之上的任何模板引擎之前更彻底地考虑他们的用例。

首先,PHP 本身非常适合模板。 使用其他模板引擎的唯一理由几乎是允许不受信任的用户创建或编辑模板,因为他们可能会执行各种恶意行为。 因此,如果您的项目有用户可编辑的模板,请使用 Smarty。 如果没有,请坚持使用 PHP。

如果您的问题是代码和布局的分离,我建议您考虑实现轻量级 MVC 风格的执行模型。 或者,更居高临下地说,如果您的模板中有更深层次的逻辑代码,那么可能是时候进行一些重构了。

性能是另一个考虑因素。 是的,渲染 Smarty 模板是有成本的。 但完成后,输出应该被缓存,从而缩短执行时间。 PHP 模板也是如此。 PHP 允许您通过使用其输出缓冲区来实现各种粒度缓存模型。 但要注意过早的优化:只有在代码完成并确定实际瓶颈是什么之后才进行优化!

使用 Smarty 或任何其他引擎时最大的成本是开发人员时间。 这是另一层复杂性,您不可避免地会发现自己处于必须欺骗引擎来完成您本来可以在纯 PHP 中完成的事情的情况。

Smarty is certainly one of the best template engines out there. In my experience though people would be well advised to think their use cases through more thoroughly before they use any templating engine on top of PHP at all.

First, PHP itself is perfect for templates. Pretty much the only justification for using another templating engine is if you're allowing untrusted users to create or edit templates, since they could execute all kinds of badness. So, if your project has user-editable templates, use Smarty. If not, stick with PHP.

If your problem is separation of code and layout, I suggest you look into implementing a lightweight MVC-style execution model. Or, to put it more condescendingly, if you have deeper logic code in your template, it's probably time to do some refactoring.

Performance is another consideration. Yes, rendering a Smarty template comes with a cost. But after it's done, the output should be cached, leading you to improved execution times. Same goes for PHP templates. PHP allows you to implement all kinds of granular caching models through the use of its output buffers. But beware of premature optimization: do that only after you're code complete and have identified what the actual bottlenecks are!

The biggest cost when using Smarty or any other engine comes in the form of developer time. It's another layer of complexity and inevitably you will find yourself in situations where you have to trick the engine into doing what you could have accomplished within pure PHP all along.

失而复得 2024-07-26 22:21:18

优点

  • HTML 文件中没有 PHP(允许 PHP 和 HTML 识别)
  • 管道 {$var|default:"None selected"} {$var|urlencode}
  • Foreachelse: {foreach item=row from=$results}{$row.name }
    {foreachelse}没有结果{/foreach}
  • 主题网站/页面(仅使用 CSS 有限制)

缺点

  • 其他语言语法 代码
  • 并不总是明显 {"Ymd"|strftime:$timestamp} {$array|@var_dump 我
  • 轻微的开销

强烈推荐“模板”方法(mVc),但是 Smarty 和普通 PHP 都可以胜任这项任务。

Pros

  • No PHP in your HTML files (Allows both PHP and HTML identing)
  • Pipes {$var|default:"None selected"} {$var|urlencode}
  • Foreachelse: {foreach item=row from=$results}{$row.name}<br>{foreachelse}No results{/foreach}
  • Themable websites/pages (Using only CSS has it limits)

Cons

  • Other language syntax
  • Not always obvious code {"Y-m-d"|strftime:$timestamp} {$array|@var_dump}
  • Slight overhead

I can highly recommend the "template" approach(mVc), but both Smarty and plain PHP are up for the task.

尝蛊 2024-07-26 22:21:18

据我所知,Smarty 是速度方面最好的模板引擎之一。 也许需要一段时间才能习惯。 但是,如果您不单独处理系统,并且 html 和样式文件的数量很大,那么它会显着加快开发速度。

在从事我的上一个项目时,设计发生了几次变化,但逻辑是相同的。 我想这是 Smarty 或任何其他模板引擎提供很大帮助时最好的例子。

As far as I know Smarty is one of the best template engines speed wise. Maybe it takes a while to get used to it. But if you are not working on the system alone and the amount of html and style files is huge it speeds up the development significantly.

While working on my last project the design was changes a couple of times but logics was the same. I guess that's the best example when Smarty or any other template engine helps a lot.

在风中等你 2024-07-26 22:21:18

我个人使用 Blitz 进行模板化。 在该网站上,作者声称它是最快的模板引擎,并提供了一个(有偏见的?)关于 PHP 不同模板系统之间性能的图表。 我自己没有使用过 smarty,但这可能会给你一些关于它性能的提示。

http://alexeyrybak.com/blitz/blitz_en.html

Personally I use Blitz for templating. On the site the author claims it is the fastest templating engine and provides a (biased?) chart over performance between different templating systems for PHP. I haven't used smarty myself, but this may give you some hints on its performance.

http://alexeyrybak.com/blitz/blitz_en.html

小梨窩很甜 2024-07-26 22:21:18

使用 Smarty 作为模板引擎的性能可能不如不使用它,因为它是一个额外的软件层,即,呃,另一种模板语言之上的模板语言。 另一方面,如果正确使用缓存功能,您可以实现整体性能提升。

Smarty 模板在输出到浏览器之前会进行预编译,这涉及将临时文件写入磁盘。 这一步肯定会影响性能,至少会影响一点。

如果您对自己将实现和表示分开的能力充满信心,并且对服务器端缓存没有真正的兴趣,那么您可能应该只使用纯 php 模板。 一些 MVC 框架(例如 Zend Framework)拥有自己的类似 PHP 的模板系统。

另一方面,smarty 是强制将表示与实现完全分离的好方法,特别是在不清楚什么属于哪里的情况下。 它可能会帮助你约束你强制执行这种必要的分离。

也就是说,我在大多数 PHP 项目中都使用 Smarty,因为它让我想起了 Java 服务器标签库 (JSTL),我非常非常习惯并且喜欢它。

Using Smarty as a templating engine will probably not be as performant as not using it, as it is an extra layer of software, i.e., a templating language on top of a, erm, another templating language. On the other hand, if you use the caching feature properly you could realise overall performance gains.

Smarty templates are pre-compiled before being output to the browser, which involves writing temporary files to disk. This step surely penalises performance, at least a little.

If you are confident in your ability to keep implementation and presentation separate, and have no real interest in server-side caching, you should probably just use pure php templating. Some MVC frameworks such as Zend Framework have their own PHP-like templating systems.

On the other hand, smarty is a decent way to enforce a neat separation of presentation from implementation, particularly where it's unclear as to what belongs where. It might help discipline you to enforce this necessary separation.

That said, I use Smarty in most of my PHP projects, as it reminds me of Java-Server Tag Libraries (JSTL), which I'm very, very used to, and fond of.

另类 2024-07-26 22:21:18

使用或不使用 Smarty 或多或少是一种哲学立场。

我使用它,尽管我没有使用太多功能。 通过这种方式使用,模板往往会非常简单。 传递带有参数的关联数组,迭代其组件并在结果页面中插入所需的元素。 这使模板保持干净并且(希望)没有业务逻辑。

此外,扩展 Smarty 非常简单。

作为示例,我向 fetch() 添加了一个 styles 参数以具有 fetchUsingStyle()。 这使我可以轻松地在网站的不同布局之间切换。

此外,我的 fetchUsingStyle() 会在各个位置搜索模板:首先尝试查找当前样式。 如果找不到,它会尝试使用默认样式加载模板。 最后,它尝试找到一个纯静态虚拟文件,这是稍后要实现的内容的占位符。

Using a Smarty or not is more or less a philosophical position.

I use it, although I don't use much functionality. Using it this way, templates tend to be be very simple. Pass an associative array with parameters, iterate through its components and insert required elements in the result page. This keeps templates clean and (hopefully) free of business logic.

Additionally, extending Smarty is quite simple.

As an example, I added a styles parameter to the fetch() to have a fetchUsingStyle(). This allows me to switch between different layouts of a site quite easily.

Furthermore, my fetchUsingStyle() searches various locations for a template: First tries to find the current style. If not found, it tries to load the template using a default style. Last, it tries to locate a pure static dummy file, a placeholder for something to be implemented later.

千笙结 2024-07-26 22:21:18

尝试使用 Smarty 和 MVC 模式(例如 Codeigniter),它比核心 PHP 更好

Try to Use Smarty with MVC pattern such as Codeigniter , Its better than core PHP

冷了相思 2024-07-26 22:21:18

当您可以只使用 html 文件并在需要的地方注入 php 代码时,为什么还要使用模板引擎呢? 你可以用 Psttt 来做到这一点! php 模板引擎

完整源代码在这里 http://github.com/givanz/psttt

Why use a template engine when you can just use your html files and inject php code where you need it? you can do this with Psttt! templating engine for php

full source code here http://github.com/givanz/psttt

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