# Slim4: how to get args for is\_current\_url() in twig

**URL:** <https://discourse.slimframework.com/t/slim4-how-to-get-args-for-is-current-url-in-twig/4273>\
**Category:** Questions\
**Created:** [July 8, 2020, 11:52am UTC](https://discourse.slimframework.com/t/slim4-how-to-get-args-for-is-current-url-in-twig/4273 "2020-07-08T11:52:31Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![odan](https://avatars.discourse-cdn.com/v4/letter/o/9de053/32.png) [@odan](https://discourse.slimframework.com/u/odan)\
**Post date:** [July 9, 2020, 8:03am UTC](https://discourse.slimframework.com/t/slim4-how-to-get-args-for-is-current-url-in-twig/4273/6 "2020-07-09T08:03:28Z")

</div>

This is “very easy” by setting the current route name as global variable. Then you can compare the route name in your twig templates.

The middleware:

```php
<?php

namespace App\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Slim\Routing\RouteContext;
use Slim\Views\Twig;

final class RouteNameMiddleware implements MiddlewareInterface
{
    /**
     * @var Twig
     */
    private $twig;

    public function __construct(Twig $twig)
    {
        $this->twig = $twig;
    }

    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
    {
        $routeContext = RouteContext::fromRequest($request);
        $route = $routeContext->getRoute();

        if ($route === null) {
            return $handler->handle($request);
        }

        $this->twig->getEnvironment()->addGlobal('route_name', $route->getName());

        return $handler->handle($request);
    }
}

```

Twig usage:

```auto
<li class="{{ route_name == 'root.page' ? 'active' : '' }}">
<li class="{{ route_name == 'user.add' ? 'active' : '' }}">
<li class="{{ route_name == 'user.view' ? 'active' : '' }}">

```

---

_[View the full topic](https://discourse.slimframework.com/t/slim4-how-to-get-args-for-is-current-url-in-twig/4273)._
