public function render($page, array $params = null)
    {
        $this->setFile($page);

        try {
            if (! file_exists($file = $this->getFile())) {
                throw new NotFoundException("Template file couldn't be found: " . $file);
            }

            if (! empty($params)) {
                $this->assign($params);
            }
            
            $template = $this->tryCompile($file);
        } catch (Exception $e) {
            $this->flushSections();

            $this->dispatchEvent('exception', $e);

            throw $e;
        } catch (Throwable $e) {
            $this->flushSections();

            $this->dispatchEvent('fatal.error', $e);

            throw $e;
        } finally {
            $this->renderingCompleted(function () {
                $this->dispatchEvent('rendering.completed');

                $this->flushSections();

                $this->onEventEmitterEnabled(function ($emitter) {
                    $emitter->removeAllListeners();
                });

                unset($this->vars, $this->shared, $this->customDirectives); // Free the memory
            });
        }

        return $template;
    }