What are the potential pitfalls of using a template function in PHP?

Using a template function in PHP can lead to potential security vulnerabilities if user input is not properly sanitized before being outputted in the template. To mitigate this risk, always make sure to sanitize any user input before using it in the template function.

function renderTemplate($template, $data) {
    foreach ($data as $key => $value) {
        $data[$key] = htmlspecialchars($value, ENT_QUOTES);
    }
    
    extract($data);
    ob_start();
    include $template;
    return ob_get_clean();
}