SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot Fiska's Avatar
    Join Date
    Jul 2004
    Location
    Dardania - Ancient Illyria
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    template engine or not?

    Hi folks,

    I'm just interested to learn when building large scale site (with a complicated CMS),in your opinion is it better using template engine (like for ex. smarty) and work around with those .tpl files, or otherwise? (never used before a template engine!)
    if otherwise define otherwise what is that otherwise? (I mean by this what do you prefer?)


    Fiska
    let's make things easier

  2. #2
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always hand code everything that I do, with just a text editor.

    Lately, I have been using template files.

  3. #3
    SitePoint Zealot Fiska's Avatar
    Join Date
    Jul 2004
    Location
    Dardania - Ancient Illyria
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by devil cat
    I always hand code everything that I do, with just a text editor.

    Lately, I have been using template files.
    I dont understand?
    I hand code too (crimson editor to be more specific), but I'm interested to see which way is better before I start this project...
    let's make things easier

  4. #4
    SitePoint Addict
    Join Date
    Apr 2005
    Location
    San Diego, CA
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're asking a very subjective question that everyone will have their own opinion. I personally use whatever works. Use whatever you think is right for you and whoever will be helping with the coding. If you're interested in smarty, the best thing to do is just go try it out. A couple hours should tell you if it's worth looking into further.
    I study speed waiting. I can wait an entire hour in 10 minutes.

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use whatever like PHPCamp said. You're going to start a huge discussion of template engine vs. no template engine here. I personally like template engines such as smarty but that in itself will raise many many arguements.

    beware.

  6. #6
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Fiska
    Hi folks,

    I'm just interested to learn when building large scale site (with a complicated CMS),in your opinion is it better using template engine (like for ex. smarty) and work around with those .tpl files, or otherwise? (never used before a template engine!)
    if otherwise define otherwise what is that otherwise? (I mean by this what do you prefer?)


    Fiska
    PHP IS a templating language (at least originally). The only real reason to use something like Smarty is in the event that the templates are going to be written by someone whom you don't want to have access to all of PHP's features (make sure you disable {php} blocks, of course) and / or full file scope (although it's QUITE possible to get around this limitation with something like Smarty).

    I generally find that it's much more logical and productive to just use PHP for everything. I do this by creating sets of "Skins" (for different looks). Each "Skin" is a collection of php files that are fed into a templating class. Variables are assigned to a Skin object, and a modified include mechanism is used to control various setup parameters. It works like this:

    PHP Code:
    class Skin
    {
        private 
    $SkinName     'Default'// The active skin
        
    private $TemplateName 'Default'// The currently rendered template        
        
    private $V         = array();    // Variables    

        /**
        *    Constructor for the page.
        *    @param string Template set to use (can be overridden)
        *    @param string The template to show.    
        */
        
    public function __construct($Skin='Default',$Template='Default')
        {
            
    $this->__set('Skin',$Skin);
            
    $this->TemplateName $Template;        
        }
        
        
    /**
        *    Internal include function (called by templates)
            *       @param string
        */
        
    private function Inc($Template)
        {
            
    $File self::GetConfig('SkinsDir').$this->SkinName.'/'.$Template.'.php';
            if(
    file_exists($File))
            {
                return include(
    self::GetConfig('SkinsDir').$this->SkinName.'/'.$Template.'.php');
            }
            else
            {
                return 
    $this->IncCommon($Template);
            }
        }

        
    /**
        *    Internal include function to load up "common" templates (those used by multiple skins)
            *      @param string
        */
        
    private function IncCommon($Template)
        {
                    if(!
    file_exists(elf::GetConfig('SkinsDir').self::GetConfig('CommonSkin').'/'.$Template.'.php'))
    {
        throw new 
    SkinException(SkinException::INVALID_TEMPLATE);
    }
            return include(
    self::GetConfig('SkinsDir').self::GetConfig('CommonSkin').'/'.$Template.'.php');
        }

        
    /**
        *    Retrieve configuration settings.
            *
            *       @param string
            *
            *       @return mixed
        */
        
    private static function GetConfig($Part)
        {        
            static 
    $Config;
            require_once(
    GLOBAL_LIB_DIR.'Config/Skin.php');
            return 
    $Config[$Part];
        }

        
    /**    
        *    Prints the current contents of the page.    
        */
        
    public function Composite()
        {                   if(
    file_exists(self::GetConfig('SkinsDir').$this->SkinName.'/'.$this->TemplateName.'.php'))
                {
                    
    $this->Inc($this->TemplateName);
                }
                else
                {
                    
    $this->IncCommon($this->TemplateName);
                }
        }

        
    /**
        *    Assigns a variable to a given name.
            *       @param string
            *       @param mixed
        */
        
    public function Assign($Variable,$Data=null)
        {
            if(
    is_array($Variable))
            {    
    // Bind a bunch of variables at once; differs from traditional  template model where the array itself would be assigned. 
                
    $this->array_merge($this->V,$Variable);
            }
            else
            {
                
    $this->V[$Variable] = $Data;
            }    
        }

        public function 
    __set($Item,$Value)
        {
            switch(
    $Item)
            {
                case 
    'Skin':                
                    
    $this->SkinName $Value;                
                break;
                case 
    'Template':
                    
    $this->TemplateName $Value;
                break;
            }
        }

    Then, a template might look like this:

    PHP Code:
    <?php $this->Inc('Header'); ?>
    <h1><?php echo $this->V['Title']; ?></h1>
    <?php echo $this->V['Body']; ?>
    <?php $this
    ->Inc('Footer'); ?>
    And the script that performs the necessary logic would be something along these lines:

    PHP Code:
    <?php
    $Skin 
    = new Skin('Skin1','Content');
    $Article CMS::GetArticle('Home');
    $Skin->Assign('Title',$Article->Title);
    $Skin->Assign('Body',$Article->Body);
    $Skin->Composite();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •