php - Extending Twig's AppVariable -
i've inherited website uses symfony 2.
the site working fine until ran composer upgrade packages. upgraded symfony 2.6 2.7.1 , twig 1.18 1.18.2.
i following error:
method "site" object "symfony\bridge\twig\appvariable" not exist in @page/page/homepage.html.twig @ line 21
the twig file in question has calls this:
{{ app.site.name }} now site not member of appvariable type. i'm having trouble figuring out how original developers managed extend twig's app global new member. don't know look. i'm not overly competent symfony.
what done old developers overriding app variable. until symfony @2.7 class called globalvariables , lives in following namespace - symfony\bundle\frameworkbundle\templating. of @2.7 it's called appvariable , lives in namespace - symfony\bridge\twig. done behind scenes?
that class containing global variables added twig global variable using method addglobal of twig , value of app inject whole class service that's defined. globalvariables or appvariables accepts service container argument , defines 5 methods default can used in our templates. show simple way of extending class, can use investigate.
create simple class define service:
<?php namespace appbundle\service; use symfony\bundle\frameworkbundle\templating\globalvariables, symfony\component\dependencyinjection\containerinterface; class superglobalsextended extends globalvariables { /** * @var containerinterface **/ protected $container = null; public function __construct(containerinterface $container) { $this->container = $container; parent::__construct($container); } } then register it:
services: app.super_globals_extended: class: appbundle\service\superglobalsextended arguments: - @service_container and last not least, override app variable of twig:
twig: debug: "%kernel.debug%" strict_variables: "%kernel.debug%" globals: app: @app.super_globals_extended now can define own methods in extended class accessing previous methods here.
for symfony @2.7 same, class name different. here extend globalvariables, @2.7 have extend appvariable.
definition of service @2.7 located in twigbundle resources folder.
<service id="twig" class="%twig.class%"> <argument type="service" id="twig.loader" /> <argument /> <!-- twig options --> <call method="addglobal"> <argument>app</argument> <argument type="service" id="twig.app_variable" /> </call> <configurator service="twig.configurator.environment" method="configure" /> </service> hope helps.
Comments
Post a Comment