如何在PHP中更好地使用Smarty? [英] how to use Smarty better with PHP?

查看:75
本文介绍了如何在PHP中更好地使用Smarty?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现将Smarty与PHP结合使用,有时需要花费额外的时间

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

1)使用与PHP本身完全不同的语法
2)需要检查小的情况,因为文档没有提供更详细的信息,例如转义"

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/zh/language.modifier.escape.php

它没有说转义:"quotes"仅适用于双引号或单引号,因此您需要编写代码对其进行测试.同样对于转义的情况:"javascript"-无法确切说明其转义内容和方式.

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)对于复杂的事情,需要编写辅助函数或修饰符,因此需要创建新文件并最终用PHP再次进行.

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.

顺便说一句,与单独使用PHP相比,使用Smarty是否可以提供良好的速度?谢谢.

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

推荐答案

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

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

我听说过使用ANY模板引擎的唯一真实"论点是它们为模板操作提供了一种更简单的语言,如果您有不了解PHP并且不认识PHP的模板设计人员,这将非常方便.信任明智地学习使用PHP.

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.

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

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;
    }

?>

我个人认为,PHP中出现了模板引擎,因为:

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

  1. 其他语言可以做到这一点
  2. 更好的PHP程序员意识到,他们需要一种在表示和应用程序逻辑之间进行分隔的方法,而模板是实现此目的的简便方法.

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

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.

我已经广泛使用Smarty了,老实说,与解决方案相比,它总是给我带来更多的障碍.如果有的话,切换到基于PHP的模板实际上减少了模板和代码的开发时间.

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.

这篇关于如何在PHP中更好地使用Smarty?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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