Because there is a learning curve, because most php template classes have their own tags or even their own language like smarty and because they are very slow.
I know that many people say that template systems are useful, but others say that they are useless piece of code.
I must agree that they are indeed useless
Why ?
There is a long list of cons:
In most php template system you must learn another "programming language" or at least some weird tags, take a look at some smarty syntax.
{if $name eq 'Fred'}
Welcome Sir.
{elseif $name eq 'Wilma'}
Welcome Ma'am.
{else}
Welcome, whatever you are.
{/if}
What is the reason of writing that piece of code when you can write this code in plain php?
<?php if ($name == 'Fred'} { ?>
Welcome Sir.
<?php } elseif ($name == 'Wilma') { ?>
Welcome Ma'am.
<?php }else { ?>
Welcome, whatever you are.
<?php }?>
Their argument? It is easier for a template designer to understand the first piece of code.
So I ask, what is so hard to understand the second one ? does "<?php" tag scare so much ?
This is the first argument, there is a learning curve, you must learn additional programming language.
The second argument is that templates are slow, they are big classes with lots of code, that parse "templates".
Smarty is an interpreted language inside another interpreted language "php" so your files (templates) are parsed two times.
Now "smarty people" will say that smarty has a cache feature that transforms smarty code to php code and this makes it faster.
The question is, why not write php code directly ? And for the caching of the page part, there are so many cache solutions these days, like APC or eaccelerator or commercial ones like Zend optimizer, and you can also have caching directly with a php class
The only use of templates I guess is in a system that you must use "untrusted" templates, that you don't wish to contain code that can harm your system, but in this case a clever apache,php configuration can do the trick. You can use php.ini to disable some of php functions and allow only basic stuff needed by templates by adding the following
disable_functions = readfile,system
#and the list can continue with other functions that you want to disable separated by comma
You will probably ask what to use in order to keep your php files clean, without any html. You can use a MVC framework to separate your presentation logic from your bussines logic, in some words php from html.
Some popular MVC (model view controller) frameworks are: Zend Framework, Symfony, Cake php and some others just search it on google
Share this with the world
Related
Comments
I'm a long time user of Smarty Templates, but I've gotta say you raise some really good points. Why do I use Smarty? To separate business logic from presentation logic. It used to be so designers could work with me, but I end up having to create the template myself anyway because web designers don't understand templates.
Posted on 2007-11-19 08:51:27I don't like having any HTML inside my PHP code. Even though, unless you enforce it, Smarty still allows PHP code in templates - and one project I worked on I found another developer just writing PHP business logic straight into the Template Pages... What's the point?
With the way I use Smarty, there's really no reason for me not to use plain old PHP instead. In some cases I've used some pretty complicated template instructions to achieve something that would be much simpler to write in PHP.
I have considered dumping smarty in the past. Maybe it's time I actually tried working without smarty on one of my sites?
Hi Michael, if your templates are not made by a designer, but by someone with php knowledge, I don't see the purpose of using smarty. I think that a designer's job is to make the template in psd or other graphics format, and the front end programmer should make the html + css from that template and finally a php programmer should include the necessary php code to the template.
Posted on 2007-09-30 15:01:48I think this approach is the best, because everybody does what he is good at.
I think is a very good idea to dump smarty, because there are no advantages in using it. You can try a template system based on php like <a href="http://phpsavant.com/yawiki/">phpsavant</a> or you can try a full featured mvc framework, the advantage of mvc is that it keeps your code really organized, keeping your business logic separated from your presentation logic.
There is indeed no purpose to use any template system in php. PHP itself is a template language, for example you can write the code above like this:
Posted on 2007-11-19 08:52:14<?php if($name == 'Fred'): ?>
Welcome Sir.
<?php elseif: ?>
Welcome Ma'am.
<?php else: ?>
Welcome, whatever you are.
<?php endif; ?>
Remember that PHP also has an alternative syntax(so you can dismiss those curly brackets )
if:
//block
endif;
foreach($items as $k=>$v):
//block
endforeach;
and the list goes on(for/switch/do/while ...)
PHP itself is a fully fledged template language.
Hi all
Posted on 2008-08-01 23:14:43@ hari : I am totally agree with you, i dont know whats wrong with the other guys, some how they mentioned some of thier logic unclear, If you know smarty its like you can use smarty, what smarty is ? actully nothing else but php, but its usage give lots of advantages like MVC based approach, real time caching, easy to reuse, easy to debug and much more..., anyway I will never avoid Smarty in my professional career.
wish you good luck.
This is an absolutely ridiculous argument for larger projects because with templating systems you can avoid a lot of code duplication by keeping PHP separate from the HTML files.
Posted on 2008-03-21 14:02:43It's obvious you've never worked on any PHP application which involves considerably more thought process than "Hello World".
Even if you avoid "Smarty" or templating systems in general, you would still find yourself writing a simple templating library yourself to keep having to repeat code segments in PHP. And trust me, large chunks of PHP code in HTML can be as confusing as hell after a while.
Well, the main reason to use Smarty (as was stated many times above) - to separate application logic from presentation logic.
Posted on 2008-03-21 14:02:43Yeah, right, you can write:
<?php if ($name == 'Fred'} { ?>
Welcome Sir.
<?php } elseif ($name == 'Wilma') { ?>
Welcome Ma'am.
<?php }else { ?>
Welcome, whatever you are.
<?php }?>
But ussually there is a whole bunch of pure php code above that.
As alternative you offer MVC frameworks. Well, are they better than template engines? Maybe, or maybe not. There is the same learning curve and many other problems. So, you can't deny the necesity of logic separation. And that's why Smarty is not bad.
as you have all said, the purpose of templating engines is to separate presentation code from logic.. the problem is, the example above combines logic with presentation.. you can really see how useful smarty can be if you actually follow the rules and use it as it was meant to be used.. for example, in your php code, lets call it example.php:
Posted on 2008-03-21 14:02:43$name = db->getName(); //whatever...
//NOW we do our logic, which is determining the greeting to use
if ($name == "Wilma") $greeting = "Welcome Ma'am.";
else if ($name == "Fred") $greeting = "Welcome Sir.";
//and then we assign our greeting
$smarty->assign('greeting', $greeting);
//finally we display the template
$smarty->display('example.tpl');
so in our template file, along with all our html, we just need:
{$greeting}
that's it..! much easier for a third-party designer to avoid (and not destroy!)
the smarty syntax is incredibly simple, you can learn to use the basic features in a day.. not to mention it provides many plugins that offer pagination (extremely useful) amonst many other things.. or you can write your own plugins of course..
MVC frameworks are a fine alternative but you can't really beat the simplicity and flexibility of smarty.. i also agree with hari and michael phipps - it is my opinion that any html combined with the php means your website is actually just webshite =)
The argument that "Smarty separates PHP from HTML files" is extremely absurd and ridiculous. What Smarty does is REPLACE the PHP in HTML files with it's own syntax which mimics PHP's, so the problem remains. Same crap, different smell. So instead of designers learning PHP, they have to learn Smarty which is only marginally simpler (notice: SIMPLER and NOT EASIER).
Posted on 2008-05-01 14:22:23Separation of application logic from view can be achieved by various OTHER means far better than Smarty, such as MVC implementation or a better, cleaner, faster, templating engine.
And what most people fail to realize is that whatever Smarty does, you can do with PHP exactly the same way.
I invite you to search the web for the Savant templating engine. It's probably the prime example the ideal templating engine. Simple, fast, unobtrusive, and uses PHP native syntax to achieve all that Smarty can do, and more, with less resources.
Yes -- obviously to those who say 'seperating the php & html is not needed', obviously you need to go back to design school, because I would never let you come close to touching one of my projects. (not even smell it).
Posted on 2008-06-22 21:25:22Smarty is absolutely fantastic at splitting html from php. Sure, smarty has its own language but seriously... if you have troubles understanding smarty syntax, you should not be doing php either, because obviously you are an amateur at php (of the millions running around claiming to be 'good programmers').
Sure, the templates contain a bit of logic (loops, if statements and the like), however this should be kept straight to the idea of only being based off of display.
Smarty has proven to be extremly useful to me over the years as business logic and presentation logic are truly separated. I don't care about the compiled templates and they are 'php again', because those are not what I edit.
so funny.... to many amateur programmers running around... Its the one disadvantage to PHP. PHP is easy to become a novice in, however it takes a lot of time to be an expert in. PHP lets its programmers get away with too much... I mean honestly... not even initializing variables before use. That is basic programming stuff! If you don't do that at the minimum... well... its time to start leaving the amateur zone of PHP and brush up your skills!
Amen Drew! You and unit.zero sound like you're the only ones who know what you're talking about.
Posted on 2008-07-21 14:51:57Anyone can learn how to make a website in PHP, but that doesn't make that person a programmer, and definitely not an expert on code design principles! Even after getting a 4 year college degree in CS and having experience programming in PHP/MySQL, Java/JSP/Servlets, C#/.NET/MSSQL and even some C++, I feel like I'm just scratching the surface.
So save your comments for impressing people that know nothing about programming so when people want to read intelligent discussion on various topics, we don't have to wade through your crap!
Thanks so much!
Make yourself heard