How to fix Invalid template file error in Magento2 after version upgrade?

This is a very common issue you will face if you using windows machine for development.

Usual error message is:

Invalid template file: '/vendor/magento/module-theme/view/frontend/templates/page/js/require_js.phtml' in module: '' block's name: 'require.js'

So whats the solution?

In case of windows, just replace this function isPathInDirectories in vendor/magento/framework/view/element/template/file/validator.php

protected function isPathInDirectories($path, $directories)
{
    $realPath = str_replace('\\', '/', $this->fileDriver->getRealPath($path));
    if (!is_array($directories)) {
        $directories = (array)$directories;
    }

    foreach ($directories as $directory) {
        if (0 === strpos($realPath, $directory)) {
            return true;
        }
    }
    return false;
}

Reference:

https://magento.stackexchange.com/a/255585/32283