Sobre smarty y los sistemas de plantillas
| From: | Antonio Hernandez Sanchez | Date: | Tue, 08 Oct 2002 14:52:48 +0000 |
| Subject: | Sobre smarty y los sistemas de plantillas | ||
| Groups: | php.general.es | ||
| Request: | Send a blank email to general-es+get-27765@lists.php.net to get a copy of this message | ||
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) ;-)
------------
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.
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.
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.
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.
Vincent
------------
--
Antonio Hernández
Perception Technologies S.L.L.
(+34) 938466893
www.perception.es