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!
I have created extremely large estimation systems, various large shopping carts, job boards, etc. - so I'm no amateur to the PHP development environment, nor the ASP, VB, RealBasic, etc.
Posted on 2009-02-05 11:37:49Smarty Templates are NOT useful, and in fact get in the way and truly cause many issues amongst both the designers and the developers. They're confusing to just about everyone, completely unnecessary, and are time consuming to say the LEAST!
Drew, you seem extremely proud of your work and from what it sounds like, it's SOOOO good that you don't even want people smelling it more or less actually seeing it (sheesh - gimme a break).
Your comments are ridiculous and actually humorous.
For anyone out there thinking of using Smarty Templates, DON'T - you'll find yourself with loss of time and a tremendous headache.
I wish Drew the best in his amazing programming!!! Can't wait to smell his next project (wonder what that smells like...hint).
To those who use smarty for big projects; good for you. Modifing design is a pain when its repeated/mixed in the logic.
Posted on 2009-03-04 19:55:47To those who use it for tiny projects; boo. There are more people involved than just the programmers and designers. The system admins have to setup up the engines and the compilers. For what? To make the programmers job easier. It just makes it difficult for the majority to take full advantage of PHP. Ive had to give up on great apps because they had dependence my server doesn't have.
Straight code sometimes just works better. Smarty is NOT always the right answer.
The programmer, designer and the admin relationship remind me of a law. The law of conservation of energy states that the total amount of energy in an isolated system remains constant. To get an app on the web the amount of energy remains constant. You can bet if the designer is using less the programmer and admin are using more.
Smarty is flocked to now but there will be something the next generation flocks to and all our knowledge will depreciate.
I think i agree with what you said about "THOSE TEMPLATING THINGS".. Its just that you have to get involved in some weilrd kinda stuff, all in the name of advancement.
Posted on 2009-03-05 09:00:21Well, i think its kinda cool, but thats if and only if you get a very good tutorial and or material to use in learning this things.
Cheers.
The Smarty way is truly a good way - if you need it. And most projects do need it, even if they know/realize it or not.
Posted on 2009-03-21 07:33:35When to use smarty (or other template engines) :
- When designers need access to your templates
||
- When the programmer needs to be forced into good coding practices - especially separating business logic from presentation logic. This forcing is required on 95% of php-developers.
When you can skip smarty and use only php:
- No designers (by good start realizing you need theese)
&&
- The programmer understands the separation issue well enough to well..eh... SEPARATE! (the remaining 5% of php-developers)
I'm myself one amongst those 5%, but I still use smarty. Partially because of the habit, partially because of the potential that a smarty-schooled designer might find its way into the employee despite of the firms incompetent leadership and partially because in any php-development-firm, there is most likely 1 or more developers not amongst the 5%, and they need to be forced. Theese are usually the high degree advanced programmers, switching from c++ or java to our "ridiculous" little language. Theese are the ones that constantly complain about loose typing, the lack of namespaces, and the need to bother themselves' busy minds with the lowbie presentational layer called html. Theese are the ones that create themselves a function to write a BR. Theese are the ones that think KISS is just a rock band, and spend a ridiculous amount of time trying to fit everything into a service-based system, where they cant rest before the singelton-pattern is in place and where everything is XML (needed or not, smart or not). Theese are the ones using a horrible amount of time programming in their hyper-advanced system just to make a mediocre guestbook (ofc it is very safe now...). Theese are the ones with knowledge of about 2% of the built-in php functions, utilizing substr, strpos, str_replace, if and else to proposterous, idiotic levels. Theese are the ones running your company bankrupt because of efficiency troubles. Theese are the ones putting the blame on php for "letting them" make the mistakes they do. "A real programming language wouldn't allow this programming mistake!" LEARN PHP BEFORE YOU WRITE, TAKE RESPONSABILITY FOR YOUR CODE!
Frustration "out".
Well... Templating sucks.. anyway you can write your own templating engine rather understanding (hardly) someones work
Posted on 2009-04-07 06:41:22I do hope you can help me with this. How exactly can you break each word on each title with comma? Is this possible by adding a code?
Posted on 2009-04-10 13:10:55I know you can break series of titles by adding {cycle values=", , , , ,"}.
What about on a single title but would like to break each word in it with a comma.
Appreciate any response from you either on this post or via email.
Thanks.
Well i used smarty 2 years but finally back to php templates itself. Smarty was pleasant and easy to work with but i come to the conclusion that it's only adding another unnecessary layer.
Posted on 2009-05-17 15:14:35I think Smarty is great for someone who doesn't have MVC programming habbit but want to seperate business logic from template. But when you are able to write websites with MVC pattern or with the help of a framework, Smarty will not be the best solution.
A simple registry class can help you to achieve good code separation for small / medium website. Ex:
registry.php
<code>
<?php
class registry
{
private $vars = array();
public function __set($name,$value = null)
{
$this->vars[$name] = $value;
}
public function __get($name)
{
if(isset($this->vars[$name])) return $this->vars[$name];
else return null;
}
}
?>
</code>
logic.php
<code>
$tpl = new registry();
//do logic stuff...
//assign tpl vars
$tpl->pageTitle = 'my page title blabla';
$tpl->datas = array('x','y','z');
</code>
template.php
<code>
<h1> <?php echo $tpl->pageTitle; ?> <h1>
<?php foreach($tpl->datas as $data) { echo $data.'<br />'; } ?>
</code>
I've been using SMARTY for years and I must say that it makes web development much easier (and cleaner). I do agree that the templating language is a bit looser than it should be. But isn't php itself guilty of that as well? But if you stick to the SMARTY basics (variables, loops, conditionals, includes) it's really smooth. It reminds me a lot of Perl's HTML::TEMPLATE.
Posted on 2009-05-29 13:49:50I've also used the templating solution that CAKEPHP adopts and I've found that it provides too much abstraction. And still, it litters your template with PHP tags. Simple operations are often difficult and the learning curve is much higher than that of using SMARTY.
SMARTY is also great to use when working with JQuery or even creating XML files. If used correctly, SMARTY is successful in truly separating business logic from display logic. I'm actually a bit surprised by the comments here. Hey, but whatever works for you...
what these smarty idiots do not realize is, that you can seperate logic from design with php.
Posted on 2009-06-19 12:06:27i use templates, and my template language is what? yes, php. and i only use php in templates to present dynamic content.
i have never seen such a useless, harming software and it scares me, that people are so stupid to use it.
I am heppy to find this blog.
Posted on 2009-10-14 10:18:17I am a new(ish) PHP developer and have been experimenting for a while with emgines like Smarty and i honestly failed to see the point in them.
The only good reason to learn it is in case you come across an application that has been entirely written in smarty.
In my mind, it is no different than creating a template with empty modules and simply writing files in html/php that can be plugged anywhere into the template eg
Header Header Header
code1 code2 code3
Footer Footer Footer
So all 3 pages are the same except the code that is surrounded by the template header and footer.
It could take even a PHP expert a while to figure out what on earth smarty is doing and why it is doing it that way.
For example, smarty that i have seen uses tables to position elements on a web page. That is something i did on my first website and now its a pain to maintain. Floating divs to me work much better.
I used Smarty for years. It has a huge benefit - a real html separation. Don't confuse MVC frameworks with Smarty - they are totally different products and server different purposes. And most MVC frameworks won't give you the real front-end separation, you still have html files full of <?php tags. It's messy, unorganized and on any medium to big scale project becomes unmanageable.
Posted on 2010-03-18 17:39:23Andrew wrote
Posted on 2010-03-18 17:40:51"For example, smarty that i have seen uses tables to position elements on a web page. That is something i did on my first website and now its a pain to maintain. Floating divs to me work much better."
----
you CAN write your own HTML to use whatever you want to layout a page: tables, divs, elephants....Smarty is not Dreamweaver.
Make yourself heard