介入模板渲染

问题描述 投票:0回答:2

我有一个控制器方法,我用它来“收集”要分配给模板的变量。我已经覆盖了控制器的render()方法来合并“收集”并渲染参数并将它们分配给模板。

例:

class Controller extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{
    private $jsVars = [];

    protected function addJsVar($name, $value)
    {
        $this->jsVars[$name] = $value;
    }

    public function render($view, array $parameters = [], Response $response = null)
    {
        return parent::render($view, array_merge($parameters, ['jsVars' => $this->jsVars], $response);
    }

    public function indexAction()
    {
        // collect variables for template
        $this->addJsVar('foo', 'bar');

        return $this->render('@App/index.html.twig', ['foo2' => 'bar2']);
    }
}

我刚刚升级到Symfony 3.4,它抱怨说,因为Symfony4我不允许覆盖render()方法,因为它将是最终的。

如何才能使其无缝工作,即无需定义新方法?

  • 我知道Twig全球,但这些对我没有帮助
  • 我可以使用服务来收集变量并将该服务注入到Twig但这看起来很奇怪
  • 是否有我可以听的事件,例如TwigPreRender或smth?
twig symfony4
2个回答
0
投票

您可以像这样从Twig内部渲染控制器:

{{ render(controller('App\\Controller\\YourController::yourAction', { 'args': 'hi' })) }}

文档here


0
投票

似乎没有简单的方法。

基本上有2种选择:

  • 通过扩展当前的Symfony \ Bundle \ TwigBundle \ TwigEngine来创建自己的模板引擎
  • 装饰当前的模板引擎服务templating.engine.mytwig

我选择了后者。

几点解释:

  • 我创建了服务templating.engine.mytwig decorates当前引擎templating.engine.twig。 Class将获得当前的'TwigEngine`作为输入,我会将大部分内容委托给它
  • 通过实施twig extension(或扩展\Twig_ExtensionInterface对我来说),该类也需要是\Twig_Extension。服务也需要有标签twig.extension。否则你最终会遇到诸如“找不到私人服务'资产'等错误”
  • setParameter / getParameter用于收集和返回参数
  • 然后我添加了快捷方法到我的控制器 - setJsVar
  • Twig模板还需要处理这些变量,最好是布局级别的某个位置。但这不包括在内
  • 您可以使用此解决方案来收集任意模板参数,例如,如果您想从其他方法或其他方法分配
  • 渲染后清除收集的参数是个好主意

这一切都值得吗?我不知道:)无法理解为什么Symfony团队选择首先制作Controller :: render final。但无论如何它在这里是:

TwigEnging类:

namespace My\CommonBundle\Component\Templating\MyTwigEngine;

use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Bundle\TwigBundle\TwigEngine;
use Symfony\Component\HttpFoundation\Response;

class MyTwigEngine extends \Twig_Extension implements EngineInterface
{
    /**
     * @var TwigEngine $twig Original Twig Engine object
     */
    private $twig;
    /**
     * @var array $parameters Collected parameters to be passed to template
     */
    private $parameters = [];


    /**
     * MyTwigEngine constructor.
     *
     * @param TwigEngine $twig
     */
    public function __construct(TwigEngine $twig)
    {
        $this->twig = $twig;
    }

    /**
     * "Collects" parameter to be passed to template.
     *
     * @param string $key
     * @param mixed $value
     *
     * @return static
     */
    public function setParameter($key, $value)
    {
        $this->parameters[$key] = $value;
        return $this;
    }

    /**
     * Returns "collected" parameter
     *
     * @param string $key
     * @return mixed
     */
    public function getParameter($key, $default = null)
    {
        $val = $this->parameters[$key] ?? $default;

        return $val;
    }

    /**
     * @param string|\Symfony\Component\Templating\TemplateReferenceInterface $name
     * @param array $parameters
     *
     * @return string
     * @throws \Twig\Error\Error
     */
    public function render($name, array $parameters = array())
    {
        return $this->twig->render($name, $this->getTemplateParameters($parameters));
    }

    /**
     * @param string $view
     * @param array $parameters
     * @param Response|null $response
     *
     * @return Response
     * @throws \Twig\Error\Error
     */
    public function renderResponse($view, array $parameters = array(), Response $response = null)
    {
        return $this->twig->renderResponse($view, $this->getTemplateParameters($parameters), $response);
    }

    /**
     * @param string|\Symfony\Component\Templating\TemplateReferenceInterface $name
     *
     * @return bool
     */
    public function exists($name)
    {
        return $this->twig->exists($name);
    }

    /**
     * @param string|\Symfony\Component\Templating\TemplateReferenceInterface $name
     *
     * @return bool
     */
    public function supports($name)
    {
        return $this->twig->supports($name);
    }

    /**
     * @param $name
     * @param array $parameters
     *
     * @throws \Twig\Error\Error
     */
    public function stream($name, array $parameters = array())
    {
        $this->twig->stream($name, $this->getTemplateParameters($parameters));
    }


    /**
     * Returns template parameters, with merged jsVars, if there are any
     * @param array $parameters
     * @return array
     */
    protected function getTemplateParameters(array $parameters = [])
    {
        $parameters = array_merge($this->parameters, $parameters);

        return $parameters;
    }
}

装饰服务(services.yml):\ t

services:
    templating.engine.mytwig:
        decorates: templating.engine.twig
        class: My\CommonBundle\Component\Templating\MyTwigEngine
        # pass the old service as an argument
        arguments: [ '@templating.engine.mytwig.inner' ]
        # private, because you probably won't be needing to access "mytwig" directly
        public:    false
        tags:
            - { name: twig.extension }

基础控制器更改:

namespace My\CommonBundle\Controller;

use My\CommonBundle\Component\Templating\MyTwigEngine;


abstract class Controller extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{
    /**
    * Allows to set javascript variable from action
    *
    * It also allows to pass arrays and objects - these are later json encoded
    *
    * @param string $name Variable name
    * @param mixed $value - string|int|object|array
    *
    * @return static
    */
    protected function setJsVar($name, $value)
    {
        /** @var MyTwigEngine $templating */
        $templating = $this->getTemplating();
        if (!$templating instanceof MyTwigEngine) {
            throw new \RuntimeException(sprintf(
                'Method %s is implemented only by %s', __METHOD__, MyTwigEngine::class
            ));
        }

        $jsvars = $templating->getParameter('jsVars', []);
        $jsvars[$name] = $value;
        $templating->setParameter('jsVars', $jsvars);

        return $this;
    }

    /**
     * Returns templating service
     * @return null|object|\Twig\Environment
     */
    private function getTemplating()
    {
        if ($this->container->has('templating')) {
            $templating = $this->container->get('templating');
        } elseif ($this->container->has('twig')) {
            $templating = $this->container->get('twig');
        } else {
            $templating = null;
        }

        return $templating;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.