[wp-trac] [WordPress Trac] #21791: Enhancement: locate_template() support for multiple file types (not just .php)

WordPress Trac wp-trac at lists.automattic.com
Tue Sep 4 14:59:29 UTC 2012


#21791: Enhancement: locate_template() support for multiple file types (not just
.php)
-------------------------+-----------------------------
 Reporter:  benallfree   |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:
-------------------------+-----------------------------
 I have modified locate_template() to allow location of file types other
 than .php. The core currently only searches for .php extensions for
 template file names (search.php, 404.php, etc). I have a HAML parser, and
 this mod allows the core to locate .haml files (search.haml, 404.haml).
 Then I can use the 'template_include' filter to handle that file type.

 Do you think this is the best way to accomplish it? I also made the
 function a little more DRY.

 {{{
 function locate_template($template_names, $load = false, $require_once =
 true ) {
   $located = '';
   $template_paths = array(
    STYLESHEETPATH,
    TEMPLATEPATH
   );
   $template_file_types = apply_filters('template_file_types',
 array('php'));
   foreach ( (array) $template_names as $template_name ) {
     if ( !$template_name )
       continue;
     foreach($template_file_types as $template_file_type)
     {
       $template_name_try = str_replace('.php', '.'.$template_file_type,
 $template_name);
       foreach($template_paths as $template_path)
       {
         $template_name_try = $template_path . '/' . $template_name_try;
         if ( file_exists($template_name_try)) {
           $located = $template_name_try;
           break 3;
         }
       }
     }
   }

   if ( $load && '' != $located )
     load_template( $located, $require_once );

   return $located;
 }
 }}}
  you think this modification would be a good core enhancement?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/21791>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list