Skip to content

[SF Migration] Allow injection of extra variables inside twig #13462

@matks

Description

@matks

Usecase: developers can override twig templates in order to modify a page display. For example if I prefer flat design over material design, I can re-style my BO with my design preference.

What happens if, while re-styling one template, I want to display an additional information using a variable that was not passed to Twig by the Controller ? As Symfony controllers are not overridable in PS 1.7, I might have to create a new Controller even though I just wanted to add 1 single extra variable. For example I just wanted to replace

return $this->render('@PrestaShop/Admin/Configure/AdvancedParameters/LogsPage/index.html.twig', [
            'layoutHeaderToolbarBtn' => [],
            'layoutTitle' => $this->trans('Logs', 'Admin.Navigation.Menu'),
            'requireAddonsSearch' => true,
            'grid' => $this->presentGrid($grid),
        ]);

by

return $this->render('@PrestaShop/Admin/Configure/AdvancedParameters/LogsPage/index.html.twig', [
            'layoutHeaderToolbarBtn' => [],
            'layoutTitle' => $this->trans('Logs', 'Admin.Navigation.Menu'),
            'requireAddonsSearch' => true,
            'grid' => $this->presentGrid($grid),
            'extra_var' => $myExtraVariable
        ]);

Possible solutions:

  • Declare a twig global variable, but this impacts all templates, not just the one I wanted (suggestion by @mickaelandrieu)
  • Create a dedicated Twig Extension to be used in my template (suggestion by @mickaelandrieu)
  • Add a listener to KernelEVENTS::CONTROLLER and KernelEvents::VIEW and perform a hook call there to enable variables injection

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureType: New FeatureStalemigrationsymfony migration project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions