Re: Sobre smarty y los sistemas de plantillas
| From: | Andres Ferrando | Date: | Tue, 08 Oct 2002 16:06:02 +0000 |
| Subject: | Re: Sobre smarty y los sistemas de plantillas | ||
| References: | 1 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-27769@lists.php.net to get a copy of this message | ||
Hola:
como primer punto - todavía ni siquiera leí el mail :) - quiero hacer
constar algo ... discutir si un sistema de planillas sirve o no, o cual
es mejor en abstracto, y sin una caso particular, es un poco difícil ...
se parece a "qué base de datos es mejor" o "que versión de linux es
mejor" ... pero bueno, simplemente veremos algunas ventajas y
desventajas generales de estos sistemas ...
> Tal como decía en el post sobre PHP orientado a objetos, no veo la necesidad
> de un sistema de plantillas que tiene tanta funcionalidad que llega a ser
> tan complicado para los diseñadores como el mismo php.
>
> Sobre este aspecto, hay un post en un forum de un tal Vincent que a mi me
> parece muy clarificador (lo incluiré aquí para que lo lea quien quiera),
> pero se puede encontrar en la siguiente dirección:
>
> http://www.sitepointforums.com/showthread.php?s=&threadid=67849&highlight=vi
> ncent+rants
>
> (cuidado con el salto de línea)
>
> Me gustaría que los defensores de los sistemas de plantillas trataran de
> rebatir lo que Vincent dice en el siguiente texto... (AVISO: es largo, quien
> no esté interesado en el tema, ya puede pulsar la tecla de suprimir) ;-)
aclaración: no me considero un "defensor de los sistemas de plantillas",
aunque sí me resultaron útiles en muchos casos.
Voy a contestar las preguntas y respuestas del final, que son como un
resumen de todo lo que dice, ya que en el resto hace varias
consideraciones personale, y repite varias cosas ...
> ------------
>
> I seriously believe that template engines like Smarty are a complete waste
> of our precious time. Luckily these packages are generally freely available,
> or they would be a complete waste of money as well. They do not add any
> features to PHP not already there, and by using them, dynamic web sites
> become more complex, a lot slower, and harder to maintain. That's right!
> Harder, not easier.
>
> Making these bold statements is plain silly without explaining them, so that
> is what I will do in the remainder of this rant. As this is a genuine rant,
> I may unpurposefully hurt the feelings of some well-respected people (at
> least by me they are ), and I may not always appear to be the nice guy I
> actually am...
>
> What does a template engine enable us to do? It allows us to separate
> content from business logic by supplying it with a template file. The
> template file is a normal HTML file, with special symbols (variables) in it
> instead of traditional content, and with special control structures (or:
> blocks) to influence the presentation logic.
>
> Here's an example of using a template file with Smarty:
>
>
>
> PHP:
>
> // Business code (index.php)
> require_once('Smarty.class.php');
> $smarty = new Smarty();
> $smarty->assign('title', 'Template Engine Test');
> $smarty->display('index.tpl');
>
> // Presentation (index.tpl)
> <html>
> <head>
> <title>{title}</title>
> </head>
> <body>
> <h1>{title}</h1>
> </body>
> </html>
>
>
>
>
> The following code, which is completely equivalent, runs much faster, is
> much clearer, and doesn't use a third-party template engine at all:
>
>
> PHP:
>
> // Business code (index.php)
> $title = 'Template Engine Test');
> include('index.tpl');
>
> // Presentation (index.tpl)
> <html>
> <head>
> <title><?= $title ?></title>
> </head>
> <body>
> <h1><?= $title ?></h1>
> </body>
> </html>
>
>
>
>
> A lot of you will probably say: "Yeah, right! This may work for simple
> examples as these, but for more complicated web sites, template engines are
> a real asset!" That is not true. The example I gave can easily be extended
> to larger, complex sites. Why is this possible? Because PHP already is a
> template engine. Examine all template engines you can think of, and compare
> their features with those of PHP. PHP has them all. And more.
>
> So why do people think we need a template engine? The answer to that
> question is very simple: PHP has become too complex. Back in the old days,
> when PHP4 was just PHP/FI, the language was nothing more than a simple
> template engine. It had variables and blocks, and that was about it. Now,
> after a couple of years, PHP has been added to (but nothing was removed!) -
> it now has classes - and a large function library was written. And the
> latter is where the problem lies.
>
> Pick any book on PHP from a shelf in your local bookstore, and look how
> result rows from a MySQL database are printed. (MySQL is of course the DBMS
> used in those books, which should already give you a clue about how bad the
> book is.) The mysql_-functions are used all over the place in the
> presentation layer.
>
> Here, in these forums, we have learned people to not use those
> mysql_-functions directly, but use a database abstraction layer instead.
> This makes coding simpler (no need to know all those functions for the
> various DBMS's) and when they decide to use another DBMS instead of MySQL
> (and they undoubtedly will at some point), the conversion will be painless.
> What is the result of those lessons? People no longer use those functions
> directly, so that's good. On the other hand, they still place their business
> logic all over the presentation code. That's not so good. Most people don't
> know what to do about it, untill they learn about those yukky template
> engines. No need to carry on here...
>
> At this point I also like to point out that saying "The layout must be
> strictly separated from the content" is a moot point. What is the layout?
> That's the colors, the fonts, the styles and so on. The plain HTML itself is
> the content. A couple of years ago cascading style sheets were introduced,
> and using these makes it possible to cleanly separate content from layout. I
> can change 99% of the layout of any of my sites by just changing the style
> sheet; there is no need to dive into the HTML. Only if the site needs a
> major overhaul the HTML must be changed. If your web designer tells you this
> isn't true, fire him.
>
> Another answer to the question "Why do people think we need a separate
> template engine?" is the following: the biggest part of the PHP community
> (and the ASP community, for that matter) knows nothing about software
> architecture. Most of the PHP programmers don't know what an O(n^2)
> algorithm is, or who Donald Knuth is, or why a sorting algorithm can never
> be faster than O(n log n), or how Dijkstra's shortest path algorithm works.
> If they don't know the answers to these basic questions, how can we expect
> them to know how the design a software program? I'm not saying the majority
> of the community consists of a bunch of idiots, because that certainly isn't
> true. There are lots of bright, intelligent people out there, but they just
> don't know how to write software, because that isn't in their field of
> expertise. And can we blame them? Of course not. People who use PHP aren't
> software developers to begin with.
>
> I have been programming now for about 13 years (I started when I was 12),
> and am currently graduating in University. In a couple of months, I will
> hopefully have a Masters Degree in Computer Science. This isn't meant to
> boast; it is meant to put things in perspective: I think I have the right to
> say that I know a little of what I'm talking about. I have been studying
> software architecture for many years know, and I still find it very hard.
> Programming is easy, yes. But developing software is extremely difficult.
> Don't trust anyone who tells you otherwise. Reading these forums, or
> examining PHP projects (PEAR, Smarty, Javuh, ...), I often become a little
> depressed. The questions asked are so simple; the mistakes made so stupid...
> But what can I expect? PHP wasn't built and isn't used by professional
> software developers (something I hope to become one day). The PEAR library
> is a badly designed (and badly implemented) library for several reasons. I
> have tried pointing these out several times (I had a long discussion with
> the PEAR developers), but most people simply don't get them, and praise the
> library to heaven. I try not to say something about this too often, because
> I'm likely to hurt feelings and there's no point in doing that. But then
> there are these people starting to praise these so-called 'template engines'
> and all I can think of is: "Here we go again..." Then I just have to say
> something about it. Again, I must stress that I don't blaim any PHP
> programmer personally, and that I don't think they are stupid. They are just
> working outside their field of expertise, that's all. It's like asking a
> carpenter to paint a house. He will probably do a nice overall job, but a
> real painter will undoubtedly do better.
>
> Well, this more or less concludes my rant. From now on I will be nice again
> to everybody, and I won't be saying all too bad things about template
> engines, the PEAR library, or anything else I have a strong opinion about.
> Unless you want me to, of course...
>
> Here are some questions/remarks you may have had when reading this rant, and
> my answers for them.
>
> Q: If you use variables like '$title', as in your example, all over your
> code, then the business logic becomes a mess, and there is a possibility of
> clashes between such variables.
> A: Then don't use these (global) variables. With little effort you can set
> up a simple class (or other structure) that stores the values for the page
> you are creating, allowing you to nicely separate presentation code from the
> business code.
es conveniente, también cuando usas templates, que uses esas clases
identidad, donde guardar los datos que pasas ... esto no tiene mucho que
ver ...
> Q: The 'better' template engines have a cache to make loading of pages much
> faster. Clean PHP doesn't do this, so the template engine is, in that case,
> faster!
> A: Incorrect. The cache in a template engine is needed to store the parsed
> template in some other format (Smarty uses PHP for that), which can then be
> instantly restored. With clean PHP the format is already correct, so there
> is no need to use a cache. Less and simpler lines code, faster execution.
Esto es parcialmente cierto. Smarty (el template que uso yo) tiene un
sistema de cache, que de hecho es tan bueno, que deberían considerar
usarlo, incluso si no usan los templates ... permite usar
identificadores que cachean distintas páginas, por ejemplo, por idioma,
o por css usados ... además, tiene otras ventajas, que hacen que lo que
dice sea limitado a muy pocos casos ...
> Q: Instead of using a template engine-specific language, our web designers -
> who know nothing about programming - are now required to have knowledge of
> PHP! We can't have that, now can we?
> A: Why not? The template engine-specific language is nothing more than a
> minimal programming language with an ugly syntax. It has variables,
> conditionals, loops, and maybe even functions. As PHP is a full programming
> language, it has all of these constructs, and more. But that doesn't mean
> you require your web designers to know all there is to know about PHP. The
> set of requirements doesn't change at all. Just the basics will be enough.
> The added benefit is that if they want to, your web designers can start
> experimenting with PHP (or other scripting languages) much faster.
Esto es relativo. Todos estamos de acuerdo en que sería genial si los
diseñadores manejasen php. Ahora bien, se supone que una buena
separación en tres capas, disminuye al mínimo (uses o no templates) la
necesidad de lógica (bucles, condiciones, etc) en las hojas html. Si uno
puede limitar eso, puede también dar un "subset" de php más limitado a
lo específico que necesite el diseñador, creando incluso "plugins" para
ofrecerle las funciones específicas que necesite, y con una sintaxis mas
sencilla en muchos casos ...
> Q: You said maintaining a web site generated by some template engine is
> harder to maintain instead of easier. Why?
> A: One reason is that an additional software package is involved. At least
> one person must be able to work with it. More knowledge is required. Also,
> not only the web site and PHP must be kept up to date; the template engine
> itself must be maintained as well. This means more work. Another reason is
> that the best solution for any problem is always the simplest one. Using a
> large, complex template engine where PHP itself suffices is not a simple
> solution. In other words: more work yet again.
es cierto que tenes que conocer una nueva "tecnología". Lo de
"mantenerla" es relativo, ya que mientras funcione, probablemente no le
hagas mucho "mantenimiento".
Me parece además, que en ningún momento hace mención a una de las ideas
principales de las plantillas, que es la de "reusar" el código.
Él da a entender continuamente que se puede vivir sin un sistema de
plantillas, pero da a entender, que uno construya plantillas en php. Eso
se parece a decir: "no uses una capa de abstracción de la BD, pero si
hacé una vos ... "
> Vincent
--
Andres Ferrando <anferr@mecon.gov.ar>