嵌套模板的简单PHP模板类

I would like to build and use a super simple PHP template class. I do not need to support any custom tags or anything.

Just need to be able to assign variables from outside the class and then have them parsed inside of the template file.

I then need to be able to return the parsed HTML output to a variable.

another nice feature would be to be able to nest template files inside of one another, I have seen this a long time ago and it was super simple to accomplish.

I do not have access to any of my old template class PHP code though so I could use any help in building a lightweight, fast and very simple class that does these tasks mentioned above. I do not want to use an existing template system!

In theory I believe it should work something similar to this...

//Instantiate a Template object
$template = new Template("path/to/template.php");

// Set some Template variables that can be accessed inside the template file
$template->title = "Hello world!";
$template->description = "This is a ridiculously simple template";
$template->posts = array();

// Return the processed template file to a variable
$outputHtml = $template->render();

The template file itself might be like this...

<html>
    <head>
        // Header scripts and content
    </head>

    <body>
        // Body content
        <h1><?php echo $this->title; ?></h1>

        <div><?php echo $this->description; ?></div>

        <?php
        print_r($this->posts);
        foreach($this->posts as $key => $value){
            echo $key;
            echo '<br>';
            echo $key;
        }
        ?>
    </body>
</html>

And a PHP template class may look something like this....

class Template
{
    protected $template;
    protected $variables = array();

    public function __construct($template)
    {
        $this->template = $template;
    }

    public function __get($key)
    {
        return $this->variables[$key];
    }

    public function __set($key, $value)
    {
        $this->variables[$key] = $value;
    }

    public function render()
    {
        extract($this->variables);
        chdir(dirname($this->template));
        ob_start();

        include basename($this->template);

        return ob_get_clean();
    }
}

So anything I can do to improve this? Also make nesting of templates inside of templates to work better?

i think it's better way to designing a PHP template

I know this is post is from 2014, but i was facing the same problem with a template class, here is how i was able to solve my problem:

This is the controller: index.php

<?php require 'core/init.php' ;

// Get template and assign vars
$template = new Template('templates/frontpage.php');

// Assign Vars
$template->title = "Hello world!";
$template->description = "This is a ridiculously simple template";

echo $template;

The template class: Template.php

<?php
/*
 *  Template Class
 *  Creates a template/view object
 */
class Template {
    //Path to template
    protected $template;
    //Variables 
    protected $vars = array();

    /*
     * Class Constructor
     */
    public function __construct($template){
        $this->template = $template;
    }

    /* __get() and __set() are run when writing data to inaccessible properties.
     * Get template variables
     */
    public function __get($key){
        return $this->vars[$key];
    }

    /*
     * Set template variables
     */
    public function __set($key, $value){
        $this->vars[$key] = $value;
    }

    /*
     * Convert Object To String
     */
    public function __toString(){
        extract($this->vars); // extract our template variables ex: $value
        // print_r($this->vars ) ;  testing
        chdir(dirname($this->template));
        ob_start(); // store as internal buffer

        include basename($this->template);  // include the template into our file

        return ob_get_clean();
    }
}

The view: frontpage.php

<!DOCTYPE html>
<html>
<head>
    <meta charset=utf-8 />
    <title></title>
    <link rel="stylesheet" type="text/css" media="screen" href="css/master.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <!--[if IE]>
        <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
</head>
<body>
  <?php echo title ; ?> 
  <?php echo description ; ?>
</body>
</html>