我有一个控制器方法,用于“收集”要分配给模板的变量。我已经重写了控制器的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()方法,因为它将是最终的。
如何使它无缝运行,即不定义新方法?
答案 0 :(得分:0)
您可以像这样从Twig内部渲染控制器:
{{ render(controller('App\\Controller\\YourController::yourAction', { 'args': 'hi' })) }}
文档here
答案 1 :(得分:0)
似乎没有简单的方法。
基本上有2个选项:
templating.engine.mytwig
我选择了后者。
很少有解释:
templating.engine.mytwig
的服务templating.engine.twig
。该类将获得当前的“ TwigEngine”作为输入,我将把大部分东西委托给它\Twig_ExtensionInterface
(或扩展\Twig_Extension
对我来说足够)来twig extension这个类。服务也需要具有标签twig.extension
。否则,您将遇到诸如“无法找到私有服务“资产”等”之类的错误setJsVar
这一切值得吗?我不知道:)无法理解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;
}
}