Hi, I am thinking of using smarty in my next project but wanted to know your thoughts first? At the moment i am using a simply php include system for my sites but now feel like going more advanced and smarty seems to be the right direction. Do you think i should bite the bullet and use it on my next project? Cheers, Adam
Smarty is a great template engine.. but don't use it where it's not necessary.. if the only person maintaining the website / project is you.. it's not really necessary. If it's for someone else I would recommend separating the design from the PHP (the whole point of Smarty.. though I've seen some people include PHP in the Smarty template which still has me confused). Basically don't use it excessively for no reason, make sure you're going to use the features of it beforehand . Sorry for the mild rant.
Unless you have a specific need for Smarty, such as switching templates, don't use it. IMHO, it is a horrible system to work with when used inappropriately. I have never seen Smarty simplify code work. It just complicates things. I agree with CodyRo. Don't use it where it is not necessary. I will be working on a site today that was custom-designed using Smarty. The original developer got so lost working with Smarty that he abandoned the project and customer he was working with. I'm picking up the pieces and need to make it work properly.
For my projects I have most of the logic of the site in classes or includes. includes contain functions. Classes have methods that use the functions. Content pages then instantiate the classes and call the methods or use the functions directly. All the content pages are in their own directory and included using the index.php page. So technically if I wanted a different look I could just change the index.php file to use a different content page directory. This way mostly all the logic is contained outside the display and it's a lot less complicated to embed PHP inside the content. I don't have a complex black box that I have little control over.
Actually, as someone who comes from a more professional programming background, and who lives by separating code from content... I find Smarty no more burdensome than other "frameworks". It makes maintaining code much simpler (ie, I can keep meddlesome website owners out of code and in templates that generally are maintainable by them). As for performance, I can show you sites that I've done in Smarty that from a performance perspective you would never guess that Smarty is behind it. Obviously that's just my opinion. I deal with similar opinions everyday from my co-workers who stand strong behind their opinion that any website of any significance would not be done in PHP (Java and .net geeks they are!).
Performance wise, PHP can't compete with Perl. Although I prefer PHP for its simplicity. For work I use Perl. For my personal projects I use PHP. I've used Java and .Net for web-sites. I would never do it again unless I had to. And unless its possible to execute code without cpu cycles, it's impossible to add layers and not take a performance hit. Smarty adds an extra layer of interpretation which necessarily slows things down even if you can't tell by looking at it as a user. The questions are then a) does it matter? b) can we afford a faster CPU? And the answer for a is probably not and b, we don't need to until your site is getting a large number of hits. Then those fractions of a second start to add up. Personally I come from a game programming background so I code sites like I'd code a game.
I think i am going to go for it to be honest. It won't harm me in learning it anyway and they i can decide to use it in future projects if needed.
Well Smarty turns it's .tpl files into cached .php files with php syntax you'd use in your seperate .php file like templates. Smarty is great but a lot of people just don't use it properly. Using it to do intense logic then you're doing it wrong. To seperate your HTML and maybe the odd if statement or looping for reporting data not so bad. It will make things much easier in the future and maybe someone without intense php knowledge could figure it out.
Easy?? my a** i've done it a couple of months ago, and it took my about 3 days to get nested loops working. I'd go for a template engine myself. I'm sick of mixing html and php code in the same page. It's a real pain in the ass if you have to change your layout when it is mixed with php code. I recently started using templates, and it really saves an awful lot of time when it comes to redesigning.
One that can do things that Smarty can (if statements, loops ect.) along with it using a caching system so it does not parse the .tpl files every time not soo much. No point reinventing the wheel on this one there are plenty out there already.
For personal things, I just mix in my PHP with my HTML. I'll be the only one using it, and I understand it, so no need to overly complicate things. For non-personal items, I almost always use Smarty. It's a very powerful engine, but it can be very basic as well. Caching is extremely simple. And most non-programmers can grasp the concept of Smarty Snytax. I don't honestly see a point in creating your own. No need to reinvent the wheel. And if you want something small, just use the basic features of Smarty. I found it to be the best templating engine all around.
I have found that writing "my own" templating tool for clients is a recipe for ongoing maintenance. Now that can be good or bad depending if you are trying to have a steady stream of work or not! Inevitably, the site owner will want to tweak it... and sometimes those tweaks will be more than the simple templating will handle. If you are going to use templating, I would say my belief on best practices are: 1. Do all programming in PHP (absolutely separate code from content wherever possible) 2. Use the templating tool of choice (Smarty in this case) only for HTML, looping, and content presentation 3. Use a CMS to allow your client to update content -- but do not stick every piece of content in to the CMS (content that basically never changes wastes cycles and makes a site look slow when loading from a CMS ) 4. Teach your client how to do basic template edits (HTML only) -- OR -- get them to sign a maintenance contract for HTML edits