Search code examples
shopware6

Detect index page on Shopware


How can I render some html based on the fact whether the home (landing) page is shown in Shopware?

I tried with various smarty ifs, but none of them worked.

Javascript is not an option - it does look ugly if the layout gets a major change in $(document).ready


Solution

  • The best way I can think of is adding a Subscriber to the page load event and then adding a page extension with a specific variable that you will use in the if statements of your Twig templates.

    I am not sure what you mean by "index page" but I think you're trying to detect if the page is the homepage?

    In this case you can check if the route is equal to the Shopware frontend.home.page route:

    $currentRoute = $request->attributes->get('_route');
    $page->addExtension('isHome', $request->attributes->get('_route') == 'frontend.home.page');
    

    Now it will be possible to check if the current page is the homepage in Twig by using:

    {% if page.extensions.isHome %}
        // Do your stuff here
    {% endif %}
    

    Alternatively you can also access the request object in the template directly:

    {{ app.request.attributes.get('_route') }}