I m planning to develop a web portal and i need some expert opinion on which approach to take. Considering the site gets a huge no. of hits, i have to worry about the site performance as well. The following 2 links encouraged me to take the smarty approach, however i would like to hear your opinion as well. http://www.phpinsider.com/smarty-forum/viewtopic.php?p=40905 http://www.sitepoint.com/article/smarty-php-template-engine 1. So should i code the web portal in pure PHP (using includes etc) or should I use smarty (which will be much helpful in terms of maintainence) ? 2. Which is faster from you own experience ? I would appreciate any advice/suggestion.
Smarty is excellent and I've seen it used in Joomla etc in preference to their standard methods. I've even seen it used with phpCake. Personally, I'd be looking deeper first and considering if something like Cake would be a good engine for the site. Templating is always a good idea though.
PHP for the scripting, Smarty for the presentation logic. And make sure there is no html in your php, and no application logic in your smarty templates.
Pure PHP is slightly faster, but I wouldn't say significantly. I love Smarty, and it can be very helpful, specially for big projects. Smarty uses a "function on demand" system (or however you want to call it). That means the files are only included if they're actually needed. You can also use template caching to save some memory and make it somewhat faster. But I wouldn't worry too much about the performance of Smarty. I highly doubt you'll face any performance problems.
Thank you very much for your inputs. Repped. Thank you nico for the elaboration. I have always used smarty for all my projects, i found it excellent as well. However, i haven't used the "cache" so far. I heard it would display the same page until the cache expires. My site will be fully dynamic. Heres how it will be: [*] First section will be a list of products and its details (very unlikely to change ) but at the bottom of the page there will be user comments (which will change - newest comment first). [*] Second section will more like a custom bolg/forum where people will discuss any problem related to any product. Do you recommend me to use cache ? I could really use your input. Anyone else want to post about smarty performance, feel free to post your inputs.
I think you can cache specific templates (it's been a while since I used the cache function). So you could tell Smarty to parse only the files that are dynamic, and use the other files from the cache. So if you'd use a separate template for the comments part, you could do it this way. I suggest you take a look at their FAQ. (Specially point 5).
Smarty templates are generated into .php files the first time they are parsed. After this the speed difference is negligible at best. So if you were to build your own template engine which did straight PHP I doubt it would be any faster than smarty if it was designed properly. I'm sure it be fast if you mixed and matched logic but then it's a headache to maintain. I would advise a template engine whether it's smarty or something else they may be slightly slower but in the long run they'll be significantly easier to maintain.
Thanks for the advice InFlow. However one thing i need to clarify, $get_cate = "SELECT ct_id, ct_name, ct_image, ct_desc, ct_link_text FROM category ORDER BY ct_id"; $result = mysql_query($get_cate); $s=1; while($cate_row = mysql_fetch_array($result);) { $cate_row_pass[$s] = $cate_row; $s++; } $smarty->assign('cate_row',$cate_row_pass); Code (markup): Then in smarty, {section name=caterow loop=$num_elements start=1} <td class="homeCategoryBlockTD"> <table class="homeCategoryBlockTABLE" border="0" cellspacing="0"> <tr> <td class="homeCategoryImageTD"> <a class="homeCategoryLinkA" href='../php/item.php?ctid={$cate_row[caterow].ct_id}'> <img align="middle" border="0" src='{#phpToMainImage#}{$cate_row[caterow].ct_image}' width="120" height="153" /> </a> </td> </tr> <tr> <td class="homeCategoryDescTD">{$cate_row[caterow].ct_desc}</td> </tr> <tr> <td class="homeCategoryLinkTD"> <a class="homeCategoryLinkA" href='../php/item.php?ctid={$cate_row[caterow].ct_id}'>>>{$cate_row[caterow].ct_link_text}</a> </td> </tr> </table> </td> {if $smarty.section.caterow.index mod 2 == 0} </tr> <tr> {/if} {/section} Code (markup): So isn't the loop running twice once in PHP and then another time in smarty ? Is it a large overhead considering there are lot of items in DB ?
Most importantly, is this a portal that will be released to people or just a project for yourself to use? If it's for yourself, I would skip smarty. Work out some sort of templating class to seperate code from presentation. If this is something you plan to release to the public or even sell, I would definetly go with Smarty or something similar, unless you can work out a badass templating class. And sarahk, where did you see Joomla use Smarty? They use Pattemplate in the core and simple functions for calling menus and mainbody?
I would use pure PHP, but I would separate it into the logic and presentation, (it would be a small smarty like php). E.g. you would set the fields that you want to present in the presentation using an array and then include a php file that is the presentation layer, which uses array with such names. I don't see a value added in templating engines, they are just another thing that you need to learn (and have a strange syntax) and can make an error. You could also use Savant (http://phpsavant.com/yawiki/) which is based on pure PHP, should be fast and doesn't add wierd syntax.
Use Google, I haven't actively worked with Joomla for about a year, but back then the forums would always have someone wanting to be different and integrate smarty. It's the same with Cake, they have a good MVC structure and people still want smarty.
The loop is running twice in the sense one time you loop the results of the mysql data set then next time you simply loop an array. The speed would be slower than say looping once but would it matter? Unlikely unless you make a big deal out of 0.000000000001s execution time difference. At times you sacrifice speed for development time and maintainability. Writing object oriented code is slower than straight procedural but you make this sacrifice to make it easier to maintain the code. If the odd speed decrease was a major deal we'd be writing everything in C or even worse assembler (although modern C compilers optimize just as well as someone doing it in assembler unless they're really really good). Fact is computers have got so fast that our issues stem on more developing the application than worrying about a small difference in CPU or memory usage.