DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

The World's Simplest (decent) PHP Templating Engine...

07.22.2005
| 1142 views |
  • submit to reddit
        ...is PHP, so why not use it?
function include_view($__view, $vars=NULL) {
    # Start buffering the generated text.
    ob_start();

    # Process the view.
    if (!is_null($vars)) {
        extract($vars, EXTR_OVERWRITE | EXTR_REFS);
    }
    include("views/$__view.php");

    # Grab the generated content and clean up.
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}
    

Comments

Snippets Manager replied on Mon, 2012/05/07 - 2:13pm

I've used a more complex version of this on a large community website. It works really well. Definately recommended.