Smarty / Seperating Code from Presentation
Moderator: General Moderators
Smarty / Seperating Code from Presentation
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
I'll answer that with two different answers: I use Smarty, but I don't use it to seperate code from presentation.nigma wrote:How many of you guys use Smarty to seperate your 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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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 ?
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 ?
Imagine a book where every other line was from another book. Then imagine trying to edit the plot of the first book.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?
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Of course, Smarty is not the only solution, and if you have enough discipline, you could:
The benefit of using Smarty is that you can let untrusted parties edit the templates.
Code: Select all
<html>
<head>
<title><?php echo $des->title; ?></title>
</head>
<body>
etc...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.
-
Simulacrum
- Forum Newbie
- Posts: 13
- Joined: Wed Apr 13, 2005 11:58 pm
Templating is a good idea
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?
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?
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
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.
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
Basically, what I said in my first post in this thread: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?
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.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
-
reverend_ink
- Forum Contributor
- Posts: 151
- Joined: Sun Apr 20, 2003 1:18 am
- Location: Las Vegas | London
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.
From a purely economical stand point, SMARTY is my only way forward.