PHP simple template loader This is not really a question but I'd like to obtain some opinions about this simple class. For an old project I had the necessity to load some php template files that includes some foreach() and for() loops and other variables loaded from a database. I was start thinking how to obtain this using a templating class and this is what I wrote to achieve the objective, I've reworked a templating class that works well with html and php files that doesn't need to load data from variables. I know that I can also use simply the include, require or include_once and require_once but writing a reusable class was the solution that seemed to me more reliable. Can this class to be considered useful? Is it possible to improve it by using magic methods like __set() and __get() to output the desired data to a loop? In the usage example I will not post the $data variable that is an array of data loaded from database, this choiche is because I don't want to write a long post.

&lt;?php

class Template{

    private $tpl;
    private $fileName;
    private $templatePath;

    public function __construct($templatePath){
        $this-&gt;templatePath = $templatePath;
    }

    public function loadTemplate(string $tpl){
        $this-&gt;fileName = basename($tpl.'.php');
        if(file_exists($this-&gt;templatePath.'/'.$this-&gt;fileName)){
             return $this-&gt;templatePath.'/'.$this-&gt;fileName;
        } else {
            throw new Exception('Template not found.');
        }
    }

}

?&gt;


Usage example: 

&lt;?php

require_once 'Template.class.php';
//$data = array(); this array comes from a db
//$results = array(); this is an handmade array
$template = new Template('path/to/template/file');

include($template-&gt;loadTemplate('demo'));
?&gt;


Template file that is loaded

          &lt;div class="wrapper"&gt;
            &lt;?php for($i = 0; $i &lt; count($data); $i++): ?&gt;
              &lt;span&gt;&lt;small class="text-uppercase"&gt;&lt;?php echo $data[$i]['h']; ?&gt;&lt;/small&gt;
              &lt;small&gt;&amp;nbsp;-&amp;nbsp;&lt;/small&gt;
              &lt;small class="text-uppercase"&gt;&lt;?php echo $data[$i]['a']; ?&gt;&lt;/small&gt;
              &lt;p&gt;&lt;?php echo $results[mt_rand(1,12)]; ?&gt;&lt;/p&gt;&lt;/span&gt;
            &lt;?php endfor;?&gt;

          &lt;/div&gt;
This is not a template class at all, but sort of a wrapper for file_exists() function. It does nothing of the templating business but just gets you a filename.

To make it a template class, 


make it accept an array with all data used in the template
add a render() function that will extract data array into separate variables and then include the template file.
add some sort of auto-escaping feature (like going through the data array recursively and escaping all scalar values)
a possibility to return the rendered HTML instead of outputting it
add some facility to call a "master" template that will add the common site design to the certain page's template.


So a usage example would be

require_once 'Template.class.php';
$data = [
    'data' =&gt; $data,
    'results' =&gt; $results,
];
$template = new Template('path/to/template/file');
$template-&gt;render($data);


Quite off topic but consider a more tidy syntax for your template.

      &lt;div class="wrapper"&gt;
        &lt;?php foreach($data as $row): ?&gt;
          &lt;span&gt;&lt;small class="text-uppercase"&gt;&lt;?= $row['h'] ?&gt;&lt;/small&gt;
          &lt;small&gt;&amp;nbsp;-&amp;nbsp;&lt;/small&gt;
          &lt;small class="text-uppercase"&gt;&lt;?= $row['a'] ?&gt;&lt;/small&gt;
          &lt;p&gt;&lt;?= $results[mt_rand(1,12)] ?&gt;&lt;/p&gt;&lt;/span&gt;
        &lt;?php endforeach ?&gt;
      &lt;/div&gt;