public function get($file, $directory = null): string
    {
        $directory = (is_null($directory) ? $this->directory : $directory);
        Logger::newLevel("Loading template file '".$file."' in '".$directory."'");

        // First load the template engines
        $this->loadTemplateEngines();

        // First retrieve the filepath
        if (is_null($this->current_engine)) {
            $this->setFileFromString($file, $directory, array_keys($this->file_extensions));
        } else {
            $this->setFileFromString($file, $directory, $this->current_engine->getFileExtensions());
        }

        // Then assign some basic variables for the template
        $main_config = Factory::getInstance()->config->get('main');
        $contact_config = Factory::getInstance()->config->get('contact');
        $this->assigned_variables['wwwDir'] = $main_config->base_url;
        $this->assigned_variables['siteURL'] = $main_config->base_url;
        $this->assigned_variables['serverName'] = $main_config->server_name;
        $this->assigned_variables['adminMail'] = $main_config->administrator_mail;
        $this->assigned_variables['contact'] = $contact_config->toArray();
        $this->assigned_variables['csrfTokenName'] = Factory::getInstance()->security->get_csrf_token_name();
        $this->assigned_variables['csrfHash'] = Factory::getInstance()->security->get_csrf_hash();

        // Select an engine if one is not already selected
        if (is_null($this->current_engine)) {
            $this->current_engine = $this->getEngineFromExtension($this->getExtensionFromFile($this->file));
        }

        $this->current_engine->setDirectory($this->directory);

        // And run an Event to see what other parts have to say about it
        $event = Events::fireEvent('layoutLoadEvent', $this->file, $this->directory, $this->current_engine, $this->assigned_variables);

        // The event has been cancelled
        if ($event->isCancelled()) {
            return false;
        }

        // And refetch the data from the event
        $this->current_engine = $event->engine;
        $this->assigned_variables = $event->assigned_variables;

        Logger::stopLevel();

        // And finally run it
        if (file_exists($event->file)) {
            return $this->current_engine->get($event->file, $this->assigned_variables);
        }

        throw new LayoutException('The requested file was not found', 1);
    }