/**
 * Fetch the template, and initialise the params
 *
 * @param   array  $params  Parameters to determine the template
 *
 * @return  HtmlDocument instance of $this to allow chaining
 *
 * @since   1.7.0
 */
protected function _fetchTemplate($params = array())
{
    // Check
    $directory = $params['directory'] ?? 'templates';
    $filter = InputFilter::getInstance();
    $template = $filter->clean($params['template'], 'cmd');
    $file = $filter->clean($params['file'], 'cmd');
    $inherits = $params['templateInherits'] ?? '';
    $baseDir = $directory . '/' . $template;
    if (!is_file($directory . '/' . $template . '/' . $file)) {
        if ($inherits !== '' && is_file($directory . '/' . $inherits . '/' . $file)) {
            $baseDir = $directory . '/' . $inherits;
        } else {
            $baseDir = $directory . '/system';
            $template = 'system';
            if ($file !== 'index.php' && !is_file($baseDir . '/' . $file)) {
                $file = 'index.php';
            }
        }
    }
    // Load the language file for the template
    $lang = CmsFactory::getLanguage();
    // 1.5 or core then 1.6
    $lang->load('tpl_' . $template, JPATH_BASE) || $inherits !== '' && $lang->load('tpl_' . $inherits, $directory . '/' . $inherits) || $lang->load('tpl_' . $template, $directory . '/' . $template);
    // Assign the variables
    $this->baseurl = Uri::base(true);
    $this->params = $params['params'] ?? new Registry();
    $this->template = $template;
    // Load
    $this->_template = $this->_loadTemplate($baseDir, $file);
    return $this;
}