干预模板渲染

时间:2018-09-06 12:37:43

标签: twig symfony4

我有一个控制器方法,用于“收集”要分配给模板的变量。我已经重写了控制器的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?

2 个答案:

答案 0 :(得分:0)

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

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

文档here

答案 1 :(得分:0)

似乎没有简单的方法。

基本上有2个选项:

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

我选择了后者。

很少有解释:

  • 我创建了decorates个当前引擎templating.engine.mytwig的服务templating.engine.twig。该类将获得当前的“ TwigEngine”作为输入,我将把大部分东西委托给它
  • 还需要通过实现\Twig_ExtensionInterface(或扩展\Twig_Extension对我来说足够)来twig extension这个类。服务也需要具有标签twig.extension。否则,您将遇到诸如“无法找到私有服务“资产”等”之类的错误
  • setParameter / getParameter用于收集和返回参数
  • 然后,我在Controller中添加了快捷方式-setJsVar
  • Twig模板还需要处理这些变量,最好是在布局级别的某个位置。但这不包括在这里
  • 您可以使用这种解决方案来收集任意模板参数,例如,如果要从其他方法或其他方法进行赋值
  • 最好在渲染后清除收集的参数

这一切值得吗?我不知道:)无法理解Symfony团队为什么选择首先将Controller :: render最终化。但无论如何,它是:

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):

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;
    }
}