SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How do you override templates?

    I'm working on an in-house CMS. I'm wondering how people set up their apps to be able to override standard templates with custom ones. One way I heard about was to have a naming system where if a custom template existed, it would be used, otherwise use the standard template. Another thing I was thinking about was that this seems very similar to the problems dependency injection tries to solve. I wonder if some sort of DI container for templates that you could set up before hand would work? It seems pretty similar to the first method, but more flexible. Any thoughts?

  2. #2
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it depends on what tempalte engine you are using!

  3. #3
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    it Depends On What Template Engine You Are Using!
    Php!

  4. #4
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My take on it is that it's hard to use object oriented methods with templates, because templates are basically procedural, unless you do something fancy like using DOM trees. So breaking down templates into as small files as possible without going too far, and doing some include path magic to determine which template should be included is a pretty good way to go.

  5. #5
    Spirit Coder allspiritseve's Avatar
    Join Date
    Dec 2002
    Location
    Ann Arbor, MI (USA)
    Posts
    648
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wysiwyg View Post
    My take on it is that it's hard to use object oriented methods with templates, because templates are basically procedural, unless you do something fancy like using DOM trees. So breaking down templates into as small files as possible without going too far, and doing some include path magic to determine which template should be included is a pretty good way to go.
    Hmm... maybe I wasn't clear. I was thinking of templates as the dependency:

    PHP Code:
    $container->use ('Icebox/Blog/Templates/List.tpl')->for ('Blog/List');
    ...
    $template $container->include ('Blog/List'); 
    Which has less to do with the actual template than the code that selects which template to use. It would take the hard-coded dependency out of the view, allowing me to customize specific sites and let others use the defaults.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quickly, off the top of my head, what about something along the lines of...

    PHP Code:
    <?php
    class TemplateLocator
    {
        protected static 
    $sDefaultTemplateDirectory;
        
        protected static 
    $sCustomTeplateDirectory;
        
        public static function 
    Obtain($sTemplate)
        {
            if(
    self::isOveridden($sTemplate))
            {
                return new 
    Template(
                    
    sprintf(
                        
    '%s/%s.tpl',
                        
    self::$sCustomTeplateDirectory,
                        
    $sTemplate
                    
    )
                );
            }
            return new 
    Template(
                
    sprintf(
                    
    '%s/%s.tpl',
                    
    self::$sDefaultTemplateDirectory,
                    
    $sTemplate
                
    )
            );
        }
        
        private static function 
    isOveridden($sTemplate)
        {
            
            return 
    file_exists(sprintf(
                
    '%s/%s.tpl',
                
    self::$sCustomTeplateDirectory,
                
    $sTemplate
            
    ));
        }
    }

    $oTemplate TemplateLocator::Obtain('header');

    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    PHP/Rails Developer Czaries's Avatar
    Join Date
    May 2004
    Location
    Central USA
    Posts
    806
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just use "$view->setPath($newPath)" to change the template path immediately, and for more automated ways, I use set_include_path to dynamically set multiple include paths, in the following order:
    1) Current site modules directory
    2) Global modules directory
    3) Fallback blank template

    That way I'm not doing multiple file system checks in PHP for the template, and I don't have to change my code for how the templates are included.

  8. #8
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by allspiritseve View Post
    Hmm... maybe I wasn't clear. I was thinking of templates as the dependency:

    PHP Code:
    $container->use ('Icebox/Blog/Templates/List.tpl')->for ('Blog/List');
    ...
    $template $container->include ('Blog/List'); 
    Which has less to do with the actual template than the code that selects which template to use. It would take the hard-coded dependency out of the view, allowing me to customize specific sites and let others use the defaults.
    Yes, well, what I meant was that it's more complicated to wire up an object with depenencies, than a template, because a template is procedural. A template is basically just a bunch of code that you include, whereas an object can be injected with all kinds of different objects.
    The DI for templates would really just be a way to set an alias for a template (or did you have more advanced features in mind for it?). And to achieve that, I think it's simpler to just use the filesystem, and have files like:
    templates/default/blog/list.phtml
    templates/override/blog/list.phtml

    And then let the code that includes the template first look in "override", and if it doesn't exist there, look in "default". You can do that by just setting the include path.


Tags for this Thread

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
  •