Diego Leite
Diego Leite

Reputation: 19

Render Symfony 5 service as twig global

I'm using a service as twig global variable. In the service constructor I set a default value of the property $title. It works initially.... Twig render the property value using the command {{ service.getTitle() }} in a template file. But after update the service property by the controllers constructor and rendering the view, the value is not updated at screen. The goal is set a twig global variable by the controllers to render in all views. How to do it?

Upvotes: 1

Views: 1467

Answers (1)

smwhr
smwhr

Reputation: 683

Twig globals are setup at init time and compiled/cached for the duration of an execution.

If you want to update and be able to call things dynamically, you should create a RuntimeExtension (see documentation here : https://symfony.com/doc/current/templating/twig_extension.html#creating-lazy-loaded-twig-extensions)

Calling it from your template will be a little more expensive (but more correct !)

Upvotes: 1

Related Questions