Smarty / Seperating Code from Presentation

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Smarty / Seperating Code from Presentation

Post 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?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Smarty / Seperating Code from Presentation

Post 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.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

One hand up here for Smarty...it revolutionised the way i work!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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 ?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
Simulacrum
Forum Newbie
Posts: 13
Joined: Wed Apr 13, 2005 11:58 pm

Templating is a good idea

Post 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?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Templating is a good idea

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
reverend_ink
Forum Contributor
Posts: 151
Joined: Sun Apr 20, 2003 1:18 am
Location: Las Vegas | London

Post 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.
Post Reply