Dear Everyone,

I am considering of starting a Smarty website. Does it worth it? Is it good? Does it have any disadvantages?
Or any other personal notices?


4 Years
Discussion Span
Last Post by veedeoo

All templaters have their pros and cons, however Smarty is pretty mature, has a good following and has extensive support on its site and accross the web. So, a solid choice if you're looking to dip your big toe into templating engines. However, there are a few slicker ones out there (IMO), with the likes of RainTPL, twig and blade. The documentation on some of these may not be as good though.

WRT templating in general, for your bog-standard static pages, it has no advantages at all, unless all your pages have a consistent structure - and then maybe, but if you create dynamic pages with data from DBs, XML files etc, it can be extremely advantageous and you'll wonder how you ever did without it.

Edited by diafol



I am considering of starting a Smarty website. Does it worth it? Is it good? Does it have any disadvantages?

Like what diafol said there's pros and cons in templaters.

I been using Smarty, I think it's worth it if you used OOP with it.



Please allow me to share my views and experiences with these template engines. Things I have not tried yet, are not included in the list..

pretty much, it all depends on how big is your application, the methods of how the php data is going to be passed to the template. How important for you is the template inheritance, how much multi-dimensional arrays the template engine has to process, do you want the template to be able to cached?

Things like those I mentioned above are very important in selecting which template engine work best in your application environment.

For lightweight applications, I will probably use RAIN TPL or TinyButStrong. These two are pretty lightweight and can outperform the big template engines like twig and smarty.

Strength of lightweight template engines: 1 file, 1 class , less methods, less tags. For example, RAIN TPL and TinyButSTrong both falls into this category of being a lightweight. Another lightweight template engine that is worth mentioning is the Savant engine.

These templates have a pretty unique ways of handling arrays. The reason I must put so much emphasis on how a template engine will handle arrays, because most applications now a days relies on the contents stored in the database. To retrieve these contents the script needs to iterate through them. Some template engines does not accept a direct assignment of a database query without looping through the result first. In most cases, template engines would need to iterate through the data, using the foreach loop function.

What makes RAIN TPL and TinyButStrong powerful and worthy of your consideration?

First, both of them are mysql wrapper friendly, the database query result can be assign directly to the template and OFF they go. Just like using a pure PHP, the results are delivered directly from the query.



## let say we have a database connector class
$db = new DatabaseConnector();
$query = "SELECT name, last, email FROM members";

## instead of looping through the query result, we can assign them here.

Notice how clean the codes above? No while loop or pre-template iteration of the results. It is a direct assignment. So, the template syntax can be as simple as this

 {$value.name} - {$value.last} - {$value.email}

for a preview of TinyButStrong, Diafol and I have had a really nice talk about it here.

I am not going to mention the Savant features, because it uses PHP functions to do its job on the template files.

Now let's go for the big ones.

TWIG, SMARTY, DWOO( dwoo is no longer maintained. My last activity in DWOO community was the day I joined Daniweb.).

Pretty much all of the template engines in existence today were inspired by SMARTY. I could safely say this, because the founder or the creator of TWIG and Symfony was ONCE a non-believer of a template engine, but change his mind later as he wrote here, before attending the Zend Conference ( **I was there, and I was the youngest (15), they thought my parents had lost me at the hotel lobby and I just accidentally entered the room full of geeks :) :) **).

Both TWIG and SMARTY are very powerful template engines and are loaded with many plugins that can pretty much do anything.

Example of these plugins

1. template inheritance
2. Template side strings manipulation class... e.g. output escaping
3. Both are secured... try hacking into a site using either one of these template engines, and you won't get that very far.
4. Framework friendly
5. Cache function
6. It has vast selections of plugins (smarty).
7. Can use blocks for javascripting a needed by the template files.

TWIG lead the pack of template engines for a while, because SMARTY took a while to upgrade from version 2.x.x to version 3.x.x

Although both TWIG and SMARTY are extremely powerful template engines, they tend to load slower and use more server resources compared to the lightweight template engines. The amount of time just to get familiar with the syntax is a lot longer than using RAIN or TBS.

1. RAIN TPL == lightweight, fast, direct assignment of db query results, noparse function is an excellent thing to have.
2. TBS == SAME AS RAIN (except noparse), can handle nested multi-dimensional array, without actually reiterating them within the php file. Compatible with many database types.
3. TWIG = All of the above, seamless integration with the symfony framework, template inheritance, cache.
4. DWOO and SMARTY == these two are pretty much the same, before the smarty version 3.x.x. All of the features mentioned above.

Special capability of SMARTY and DWOO.
In smarty, you can cherry pick the items in the loop during iteration. For example, if I have 20 items in the array, and the template engine must iterate them, I can select the exact iterated item number to insert something. This is very useful for a big listing site where inserting a banner ads is highly desirerable..

Sample code with iteration control in SMARTY.. this will insert a banner ad after every 4 items within the array.

{foreach $items item}
    {if $.foreach.default.iteration == 4}
    banner ad here

Super Strenghts of SMARTY and TWIG.This what separate them from the rest.** Example codes below are lifted from my original work for the youtube search..** it is an old version, so I really don't care publishing it to the public, without any worries of being a subject of the so called "non-disclosure agreement of 2005 " . SMARTY and TWIG are perfect for application written in OOP just like what LastMitch posted above. Here is the simplest demonstration of that

public function getSearch($keywords, $filterstring, $rowsPerPage, $type){
        ## ignore data for pagination... it was move in the paginate class.
        $page = self::validate_page();
        $hide = "" ;    
        $offset = ($page - 1) * $rowsPerPage;
        $typestring = "AND (mediatype='video') " ;
        $dbFilters= "title, description, id, groupid, group_id, category, thumb,  poster, embprovider, allviews, mediatype, cat_id, cat_name";
        $keywordsearch = "AND (title LIKE '%".$keywords."%' OR description LIKE '%".$keywords."%' OR tags LIKE '%".$keywords."%')" ;
        $getItems =("SELECT ". $dbFilters ." FROM media LEFT JOIN `group` ON (`media`.`groupid` = `group`.`group_id`)LEFT JOIN `category` ON (`media`.`category` = `category`.`cat_id`) WHERE `status` ='true' ".$hide." AND (`suspended`!=1 OR `suspended` IS NULL) ".$keywordsearch." ". filter_var($filterstring, FILTER_SANITIZE_STRIPPED) ." LIMIT $offset,$rowsPerPage") ;

        $res = $this->db->fetch_array($getItems);   
        ## these are template bound
        return array(
                 'video' => $res,
                 'pageTitle' => 'Search Results for - '. $keywords,
                 'pagination' => $this->pagination->pagenate(** I remove the pagination function**,$page,$rowsPerPage, $type)

The model class file will send the above search query to the smarty engine.. like this.. NOTE: $this->veedeoo is an instance of class veedeoo extends $smarty which is the SMARTY template engine class.

    private function get_search($themeFile, $fileName){
            $filterstring = "ORDER BY allviews DESC" ;

            if(!$this->veedeoo->isCached(THEME .$themeFile, self::this_page() .$fileName)){
            ## no cached is made we execute and generate content from the database
            ## if it is a search query get keywords, filterstring, page per search result, and then assign search as type.
            ## param: method(item1,item2,item3,type).

            $this->veedeoo->assign($this->getV->getSearch(self::checkSform(), $filterstring, $this->settings->getSetting('searchpp'), 'search'));
            ## code below sends everything to the template file.
            return $this->veedeoo->display(THEME .$themeFile, self::this_page() .$fileName);

That's pretty much it.. I sincerely aplogize for such a lenghty read, but I just love to write on something that excites me and provides so much nutrients to the other readers. In this case, I am extremely excited to see people talking about template engines. It is something most people should learn how to do, So that PHP community and its practitioners shall be viewed as HIG-LEVEL programmers not as copy paster or embedder programmers, as viewed by the other programmers of different lanuguages. I wish more and more people will strive to learn OOP and then jump into the MVC frameworks.

top post veedeoo. Nice
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.