Are there best practices for creating a custom template engine in PHP?

When creating a custom template engine in PHP, it is important to follow best practices to ensure efficiency, security, and maintainability. Some best practices include separating the template logic from the application logic, using secure input/output handling to prevent injection attacks, providing clear documentation for template usage, and optimizing the template rendering process for performance.

// Example of a simple custom template engine in PHP

class TemplateEngine {
    private $templatePath;
    
    public function __construct($templatePath) {
        $this->templatePath = $templatePath;
    }
    
    public function render($template, $data) {
        $templateFile = $this->templatePath . $template . '.php';
        
        if (file_exists($templateFile)) {
            ob_start();
            extract($data);
            include $templateFile;
            return ob_get_clean();
        } else {
            throw new Exception('Template not found');
        }
    }
}

// Example usage
$templateEngine = new TemplateEngine('/path/to/templates/');
echo $templateEngine->render('my_template', ['name' => 'John Doe']);