#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Best templating engine for php


    This is one of those topics that I see so many different opinions on!

    I use Codeigniter now and I am doing a research which templating engine suits the best. I haven't found 2 post that have the same opinion! Some say smarty is gold some say stay away from it. Same with Rain tpl, twig,...

    I was wondering if you could write your comments on this.

    Thanks
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    there is no "best template engine". Some are more user friendly than others, some are faster, some use less memory, some are simpler etc.

    I'd go with one of the mainstream engines (Smarty, Twig, ...) that have been proven to work and then simply choose the one you like best. If performance is very important to you, you might also google for benchmarks.
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    Hi,

    there is no "best template engine". Some are more user friendly than others, some are faster, some use less memory, some are simpler etc.

    I'd go with one of the mainstream engines (Smarty, Twig, ...) that have been proven to work and then simply choose the one you like best. If performance is very important to you, you might also google for benchmarks.
    Thanks I made up my mind. I am going to learn Smarty.
    Benefits:
    - It seems to be important to large companies in case I want to look for job in future,
    - It is another skill,
    - And it is always good to learn something new,
    - It is well known ( as you mentioned),

    Saying that I still think the messy code in inevitable.
    PHP Code:
    {foreach $foo as $bar}
      <
    a href="{$bar.zig}">{$bar.zag}</a>
      <
    a href="{$bar.zig2}">{$bar.zag2}</a>
      <
    a href="{$bar.zig3}">{$bar.zag3}</a>
    {
    foreachelse}

    //OR
    {foreach $foo as $bar}
      {if 
    $bar@iteration is div by 4}
        </
    tr><tr>
      {/if}
      <
    td>{$bar}</td>
    {/foreach}
      
    There were no rows found.
    {/foreach} 
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Originally Posted by zxcvbnm
    Thanks I made up my mind. I am going to learn Smarty.
    Sounds good.



    Originally Posted by zxcvbnm
    Saying that I still think the messy code in inevitable.
    Well, I don't find this example all that terrible. And you'll rarely need stuff like "... @iteration is div by ...", since the structure should already be given by the model.

    But sure, you'll never be able to avoid "messy" templates completely. It simply comes with complexity.
  8. #5
  9. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Originally Posted by Jacques1
    Sounds good.
    Well, I don't find this example all that terrible. And you'll rarely need stuff like "... @iteration is div by ...", since the structure should already be given by the model.
    But sure, you'll never be able to avoid "messy" templates completely. It simply comes with complexity.
    Jacques1! I got Smarty working. I don't like it at all! Now I see how much I like the idea of using php tags within html code! Maybe I am just new to this! This is strange that everyone uses template engines. I know we talked about this but I still think the messy code is inevitable and the benefits are nothing compared to the headache. Or maybe I am missing the whole point.
    PHP Code:
    if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    class 
    Members_model extends CI_Model
        
    {
            function 
    list_members()
                {
                    
    $query $this->db->query("SELECT * FROM members");
                    return 
    $query->result();
                }
        } 
    PHP Code:
    public function list_members()
            {
                
    $this->load->model('members_model');
                
    $members $this->members_model->list_members();
                
    $this->smarty->assign("members"$members);
                
    $this->smarty->view('index');
         } 
    Code:
    {section name=co loop=$members}
     {$members[co]->email}<br />
    {/section}
    Last edited by zxcvbnm; November 20th, 2012 at 04:56 AM.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Um, you've used Smarty for two days, and you've already come to the conclusion that template engines in general are bad?

    First of all, you've picked the most cumbersome solution. Simply make a foreach:

    Code:
    {foreach $members as $member}
    	{$member->email}<br />
    {/foreach}
    Secondly, if you don't like Smarty, then simply try another engine. Wasn't that the whole point of this thread?

    I mean, nobody forces you to use template engines. If you want to use the good old PHP tags, simply do it. But making a decision based on a quick look and "this looks strange" doesn't sound like a good idea.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    8
    To be honest, I'm using a template which I found from watching a couple of youtube videos. This guys setup works really really well with what I am doing. It's all about using one main index.php page to display all your HTML template pages. Such a neat system.

    Check this out: http://www.youtube.com/user/betterphp
    Browse through his videos and you will come across templates which he writes the code for in his videos. He explains things pretty well also.

    Hope this helps.

    Regards.

IMN logo majestic logo threadwatch logo seochat tools logo