I'm trying to find a way to extend a known twig block in an event listener/subscriber.
The code is very basic.
Main twig:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Hello World</title>
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
</html>
As you can see it's just a plain html with 2 blocks: body and javascripts.
Now for example I've a event listener onKernelResponse. What I want here is to be able to add new javascript to the "javascripts" block with twig's "extends".
The code could be something like:
public function onKernelResponse(FilterResponseEvent $event)
{
if (HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) {
return;
}
$response = $event->getResponse();
//complete guesswork code to illustrate what I'm trying to achieve
$content = $this->twig->extend( //extend() function is imaginary
$response, //pass in current response
'AcmeBundle:Javascript:index.html.twig',
);
$response->setContent($content);
}
The $this->twig->extend should work like $this->twig->render, but instead of render something new, extend() should take the $response and extend it with the 'AcmeBundle:Javascript:index.html.twig' where I can add more content to the "javascripts" block.
Here is the example of the "AcmeBundle:Javascript:index.html.twig":
{% block javascripts %}
{{ parent() }}
<script>
var something = "new";
</script>
{% endblock %}
Basically I'm looking for a way to programatically extend twig block in event listeners.
Thanks for the help in advance.