public function setUp(ThemeInterface $theme, TemplateManager $templateManager, PageBlocksInterface $pageBlocks, Template $template = null)
    {
        $this->templateManager = $templateManager;
        $this->pageBlocks = $pageBlocks;
        $this->theme = $theme;

        $this->dispatch(PageTreeEvent\PageTreeEvents::BEFORE_PAGE_TREE_SETUP, new PageTreeEvent\BeforePageTreeSetupEvent($this));

        $language = $this->getLanguage();
        $page = $this->getPage();
        if (null !== $language && null !== $page) {
            $this->pageBlocks->refresh($language->getId(), $page->getId());
        }

        $options = array();
        if (null !== $language) {
            $options["language"] = $language->getLanguageName();
        }

        $templateName = "";
        if (null !== $page) {
            $options["page"] = $page->getPageName();
            $templateName = $page->getTemplateName();
        }

        $this->template = (null === $template) ? $this->theme->getTemplate($templateName) : $template;
        if (null === $this->template) {
            return $this;
        }

        $this->assetsManager
            ->withExtraAssets($this->cmsMode)
            ->setUp($this->template, $options)
        ;

        $this->templateManager
             ->refresh($this->theme->getThemeSlots(), $this->template, $this->pageBlocks);

        $this->setUpMetaTags($this->getSeo());

        $this->dispatch(PageTreeEvent\PageTreeEvents::AFTER_PAGE_TREE_SETUP, new PageTreeEvent\AfterPageTreeSetupEvent($this));

        return $this;
    }