Page 1 of 2
Smarty / Seperating Code from Presentation
Posted: Thu May 26, 2005 2:24 pm
by nigma
How many of you guys use Smarty to seperate your code from presentation? If you don't use Smarty but still seperate code from presentation how have you done it and what have you found works best?
Re: Smarty / Seperating Code from Presentation
Posted: Thu May 26, 2005 2:50 pm
by Roja
nigma wrote:How many of you guys use Smarty to seperate your code from presentation?
I'll answer that with two different answers: I use Smarty, but I don't use it to seperate code from presentation.
In reality, most of my coding is done on games that have extremely dynamic output - output that requires a fairly large amount of logic/code.
As a result, while smarty helps reduce the amount of html-in-php, its not a true seperation of code from presentation. MVC is a very strict concept, and even one or two logic branches in a template violates it (imho).
All of that said, Smarty is terrific for what I use it for: Code grouping. It makes it much easier to group (x)html seperate from php. Could I do so without Smarty? Sure.
However, I find Smarty to have all the functionality I need, fairly low overhead, nice security, and most importantly, I dont have to reinvent the wheel and code my own.
The fact that it is rapidly becoming a defacto standard, and is being actively used on major sites (parts of Yahoo even!) is just icing on the cake.
So, yes, I use Smarty, and I can't imagine coding without it anymore, but even I wouldn't say it gives true display/code seperation.
Posted: Thu May 26, 2005 3:15 pm
by JayBird
One hand up here for Smarty...it revolutionised the way i work!
Posted: Thu May 26, 2005 6:06 pm
by shiznatix
never used smarty... i just make a code and output crap tables then go back through and make nice css tables...am i missing somthing here?
Posted: Thu May 26, 2005 6:51 pm
by Ambush Commander
I'm currently switching to Smarty on my major project. It made me realize my HTML was horrendous, which meant I had to redesign my templates.
It has utterly changed the way I work. At least for big projects anyway.
Posted: Thu May 26, 2005 8:03 pm
by nigma
Right now with my website I have one main file that depending on what variables it is called with will load a different page. This works great because all I need to do to change the menu or footer for example is alter the file that they reside in, say header and then save and changes are made throughout the site. The non-static content (i.e. not the headers or footers) are stored in php files that often grab data from somewhere and output xhtml. Like I said, this works fine, but for a bigger website with lots of people working on different parts it might not work so well.
Some kind of seperation of code logic from presentation seems in order for the bigger sites. Does anyone have experience setting something like this up ?
Posted: Thu May 26, 2005 8:06 pm
by Roja
shiznatix wrote:never used smarty... i just make a code and output crap tables then go back through and make nice css tables...am i missing somthing here?
Imagine a book where every other line was from another book. Then imagine trying to edit the plot of the first book.
Thats the difference.. with smarty, its much easier to go line-by-line through the (x)html and fix errors and layout and so on.
Posted: Thu May 26, 2005 8:11 pm
by Ambush Commander
Of course, Smarty is not the only solution, and if you have enough discipline, you could:
Code: Select all
<html>
<head>
<title><?php echo $des->title; ?></title>
</head>
<body>
etc...
The benefit of using Smarty is that you can let untrusted parties edit the templates.
Posted: Thu May 26, 2005 8:19 pm
by neophyte
It took me a long time to warm up to the idea of templates and templating. I'm still not entirely converted to the idea. I still wouldn't implement it on small projects. I'm definitly warming up to it. I've implemented smarty on a project now. I've tried three templating systems Yapter, PhpBB Template Class, and Smarty. Smarty is definitly the way go with templating.
Templating is a good idea
Posted: Sat May 28, 2005 7:33 am
by Simulacrum
I agree that templating is a good idea.
But, for the people that use Smarty, what do you think of the argument "PHP IS a templating solution".
Main Article purporting this viewpoint is here:
http://www.massassi.com/php/articles/template_engines/
The basic argument is why bother learning another scripting language when you can achieve a lot of the same objectives using native PHP functions?
Posted: Sat May 28, 2005 8:34 am
by neophyte
I've read similar articles before.That's why I've not fully accepted templating engines like SMARTY before. I'm going to try that guy's simple php based templating system. Really what is the difference between <?php if(somecondidtion){ ?> <?php } ?> and {if somecondition}{/if} --- A Huge library of code? --- Honestly I don't fully understand the whole templating thing.
Posted: Sat May 28, 2005 9:09 am
by Ambush Commander
Here are some arguments for using Smarty.
1. It has built in caching, for those of you to lazy to build your own cache system Smarty does this work for you quite well.
2. Templates can be edited by nontrusted parties: Smarty has security features to disable potentially evil server scripts.
3. Smarty forces this seperation: often times, people (note the plural) don't have enough discipline to keep it seperated themselves.
But as you can see, some of these reasons may not apply to you: sometimes not using Smarty is a good idea.
Re: Templating is a good idea
Posted: Sat May 28, 2005 9:17 am
by Roja
Simulacrum wrote:
But, for the people that use Smarty, what do you think of the argument "PHP IS a templating solution".
Main Article purporting this viewpoint is here:
http://www.massassi.com/php/articles/template_engines/
The basic argument is why bother learning another scripting language when you can achieve a lot of the same objectives using native PHP functions?
Basically, what I said in my first post in this thread:
However, I find Smarty to have all the functionality I need, fairly low overhead, nice security, and most importantly,
I dont have to reinvent the wheel and code my own.
Seperating code from output is not trivial. There are times when you need logic statements (unfortunately), and other processes. To handle that, you end up coding your own template system.
Why bother? Smarty is fast, does what I want, and I don't have to code it.
To people that make the argument that PHP is a templating engine, I say in return "Why are you reinventing a wheel". Just a waste of time for very little gain, imho. If you read the linked article, he builds a subset of the Smarty functionality - manually. All to prove the point that PHP can do it without help. Duh, sure. PHP can also do ZIP handling on its own, but its much better to do it with the amazing php_zip extension.
But the most powerful argument against that claim is that I already use it in all my major projects, and to switch *off* of it would take even more time. Thats definitely not worth it.
Posted: Sat May 28, 2005 10:31 am
by RobertGonzalez
I use the phpBB template class and it works great for me. It does everything I need it to how I need it to do it.
Posted: Sat May 28, 2005 6:26 pm
by reverend_ink
I moved to smarty about six months ago and have to say it allowed me to move faster through projects because as I recieve the HTML from designers I dont have to spend as much time working on the page and making sure layout remains, and can spend that time coding.
From a purely economical stand point, SMARTY is my only way forward.