php making of django like template system with extend

add this class Template :

class Template

    const DIRECTORY = '';

    protected $path;

    protected $extendedTemplate = null;

    protected $sections = array();
    protected $currentSection = null;

    public function extend( $name )
        $this->extendedTemplate = new Template( "views/".$name );

    public function begin( $sectionName )

        ob_start(); // start output buffering
        $this->currentSection = $sectionName;

    public function end()

        if ( !is_null( $this->extendedTemplate ) )
        { // current one is a sub template

            // read buffer contents and drop them
            $this->sections[ $this->currentSection ] = ob_get_clean();

        { // current one is a master template

            if ( isset( $this->sections[ $this->currentSection ] ) )

                ob_end_clean(); // drop default content
                echo $this->sections[ $this->currentSection ];


                // output buffered data and drop buffer
                echo ob_get_clean();




// will be called from "build" later
    public function setSections( $sections )

        $this->sections = $sections;

    public function __construct( $name )

        $this->path = sprintf( '%s/%s', self::DIRECTORY, $name );


    public function build()

        // start output buffering

        // just include the template file (some error checking would be nice)
        include $this->path;

        // Now our "extend", "begin" and "end" methods can be called from the template
        // using $this: $this->extend(), $this->begin() and so on...
        // We can't do anything here, just wait until the template is included
        // Note that we capture the HTML output with the output buffering.

        // get our buffered data
         $output = ob_get_clean();
        // Now we can check what we've got.
        if ( !is_null( $this->extendedTemplate ) )
        { // we have a sub template, go on with the master template

            // Since a master template is set, the "end" method has captured the contents
            // of the template sections. Now we have to provide the master template our
            // data.

            $this->extendedTemplate->setSections( $this->sections );

            // Now just display the master template.
            return $this->extendedTemplate->display();

        { // we have a master template, nothing more to do here

            return $output;
            // Since in a master template the "end" method outputs either the default
            // content or the extended data of a section our HTML output is complete.



    public function display()

        echo $this->build();



Call like this :

 $template = new Template( "views/$c_base/$method.phtml" );



link:// thanks to guy..


About rahul23134654

Hi, I am Rahul Meha , B.E. in (I.T.)
This entry was posted in PHP. Bookmark the permalink.

One Response to php making of django like template system with extend

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s