public function setTwigEnvironment()
    {
        if (!$this->template instanceof Template) {
            return $this;
        }

        $this->template->configure($this);
        $controller = $this->getControllerName();
        $this->layout = Inflector::toDirectorySeparator($this->layout);

        if ($this->layout == '') {
            $this->layout = strtolower($controller);
        }
        $this->setViewPath();

        if (!is_object(static::$twigEnvironment)) {
            static::$twigEnvironment = $this->template->setEnvironment();
        }

        if ($this->isDebugModeOn()) {
            $this->template->addExtension();
        }
    }