简体   繁体   English

将页面变量获取到 Sonata 树枝模板

[英]Getting the page variable to Sonata twig templates

When using sonata templates, the individual pages from the PageBundle are meant to extend the page.site.layout template, then you can use twig standard block system to place things where you like.使用奏鸣曲模板时,PageBundle 中的各个页面旨在扩展 page.site.layout 模板,然后您可以使用 twig 标准块系统来放置您喜欢的东西。 However i'm finding that the page variable is undefined on my pages and i'm trying to understand how the page variable gets to them.但是我发现页面变量在我的页面上未定义,我试图了解页面变量如何到达它们。

Ive var_dump'ed page variable in multiple templates and just cant find where it is, i've tried googling and haven't found anything of interest.我在多个模板中使用了 var_dump 的页面变量,但找不到它在哪里,我试过谷歌搜索,但没有找到任何感兴趣的东西。

{% extends page.site.layout %}

I expect the page variable to be available to me within each of the sonata pages by default, i'm not sure whether I need to pass page in from sonata, I kind of thought it was handled by sonata?我希望默认情况下在每个奏鸣曲页面中都可以使用页面变量,我不确定是否需要从奏鸣曲传入页面,我有点认为它是由奏鸣曲处理的?

Hope this helps, here is how I did it.希望这会有所帮助,这是我如何做到的。

A page helper service :页面帮助服务

namespace App\Service;

use Sonata\PageBundle\CmsManager\CmsManagerSelectorInterface;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Page\TemplateManagerInterface;

class PageHelper
{
    private $cmsSelector;
    private $templateManager;

    public function __construct(
        CmsManagerSelectorInterface $cmsSelector,
        TemplateManagerInterface $templateManager
    ) {
        $this->cmsSelector = $cmsSelector;
        $this->templateManager = $templateManager;
    }

    public function getCurrentPage(): ?PageInterface
    {
        $page = $this->cmsSelector->retrieve()->getCurrentPage();
        if ($page instanceof \Sonata\PageBundle\Model\SnapshotPageProxy) {
            $page = $page->getPage();
        }

        return $page;
    }

    public function getTemplatePath(PageInterface $page): ?string
    {
        $template = $this->templateManager->get($page->getTemplateCode());
        if (null !== $template) {
            return $template->getPath();
        }

        return null;
    }
}

The Twig extension to get the current page or template (of the page):获取当前页面或模板(页面的)的 Twig 扩展

namespace App\Service;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;

class TwigPageExtension extends AbstractExtension
{
    private $pageHelper;

    public function __construct(PageHelper $pageHelper)
    {
        $this->pageHelper = $pageHelper;
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('current_page', function () {
                return $this->pageHelper->getCurrentPage();
            }),
            new TwigFunction('current_page_template_path', function () {
                return $this->pageHelper->getTemplatePath($this->pageHelper->getCurrentPage());
            }),
        ];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM