[wp-trac] [WordPress Trac] #11242: Add "template_include" action to be run just before templates are included

WordPress Trac wp-trac at lists.automattic.com
Tue Nov 24 19:55:28 UTC 2009


#11242: Add "template_include" action to be run just before templates are included
------------------------------------+---------------------------------------
 Reporter:  holizz                  |        Owner:  westi         
     Type:  enhancement             |       Status:  reopened      
 Priority:  normal                  |    Milestone:  Future Release
Component:  Plugins                 |      Version:                
 Severity:  normal                  |   Resolution:                
 Keywords:  has-patch dev-feedback  |  
------------------------------------+---------------------------------------

Comment(by harrym):

 Sure. We're using this filter to make a plugin that allows us to write
 Wordpress templates using HAML. We find HAML much easier to maintain than
 embedded HTML/PHP.

 So, we do:

 {{{
 add_filter('template_include', 'wphaml_template_include');
 function wphaml_template_include($template)
 {
    // Globalise the Wordpress environment
    global $posts, $post, $wp_did_header, $wp_did_template_redirect,
 $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

    // Is there a haml template?
    $haml_template = str_replace(".php", ".haml.php", $template);

    if(file_exists($haml_template))
    {
       // Process the HAML template to turn it into HTML
       $haml->parse_and_execute($haml_template);

       // The current HAML parser requires templates to execute in its own
 context (suck)
       // so return null to prevent WP from including the template. If this
 parser was
       // better, we'd just return the path to the compiled template here.
       return null;
    }

    // No haml template -- use Wordpress's
    return $template;
 }
 }}}

 I've cut lots out to make it simpler, but that's the general idea.

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/11242#comment:10>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list