In PHP, what best practices should be followed when parsing and including template files to prevent security vulnerabilities?

When parsing and including template files in PHP, it is important to sanitize user input to prevent security vulnerabilities such as code injection attacks. One way to do this is by using the `htmlspecialchars()` function to escape any HTML characters in the input before including it in the template file. Additionally, it is recommended to set strict permissions on the template files to prevent unauthorized access.

$template = $_GET['template']; // Assuming the template file is passed as a query parameter
$template = htmlspecialchars($template); // Sanitize the input

if (file_exists("templates/$template.php")) {
    include "templates/$template.php"; // Include the sanitized template file
} else {
    echo "Template not found.";
}