as of version 5.2 Edit this page on GitHub

Using global variables in templates

In this article we will show how to assign global variables to all frontend templates using a small plugin based on the 5.2 plugin system.

Register event

Since we want to add our global variables only to frontend templates we register to the secure post dispatches of frontend and widgets.
Attention: The plugin should not do some performance sensitive tasks here, otherwise each request in the storefront will be slowed down. Furthermore the assigned variables may not be compatible with the HTTP cache.

/**
 * @inheritdoc
 */
public static function getSubscribedEvents()
{
    return [
        'Enlight_Controller_Action_PostDispatchSecure_Frontend' => 'onPostDispatch',
        'Enlight_Controller_Action_PostDispatchSecure_Widgets' => 'onPostDispatch'
    ];
}

Our example will just add the user login status to the template.

/**
 * @param \Enlight_Controller_ActionEventArgs $args
 */
public function onPostDispatch(\Enlight_Controller_ActionEventArgs $args)
{
    $args->getSubject()->View()->assign('sUserloggedIn', Shopware()->Modules()->Admin()->sCheckUser());
}

Example Plugin

The complete code of this example plugin can be found here. Just unpack it to your custom/plugins folder inside your shopware installation and activate it in the plugin manager.