unbreak unbreak - 1 year ago 102
Twig Question

Symfony and Twig, prerender, parse view, replace content

I need replace some content in every file of view. But content in many places is just a text, not a variable, co i cant use

variable|replace()
filter.

Is there any way to get view (eg in Controller) content before displaying so I can pass it to some function and return ready to display content?
Or maybe there is way for replace all content in
block
tag?

NDM NDM
Answer Source

Symfony controller has 2 functions render() and renderView(), the former renders a view and returns a Response, the latter just renders the view and returns the string result.

so you could in your action:

$content = $this->renderView('my-view.html.twig', [...]);

$content = str_replace('my text', 'my new text', $content);

return new Response($content);