What are common pitfalls when including templates in PHP for a CMS?

Common pitfalls when including templates in PHP for a CMS include not properly sanitizing user input, not using a secure method to include files, and not properly handling errors or exceptions. To solve these issues, always sanitize user input before including it in a template, use a secure method such as realpath() to include files, and implement error handling to prevent potential security vulnerabilities.

// Example of including a template with proper sanitization and error handling

$template = 'template.php';

if (file_exists($template)) {
    include realpath($template);
} else {
    echo 'Template not found.';
}