protected function getTemplate(Request $request, $controller)
    {
        if ($request->attributes->has('_template')) {
            return null;
        }

        $format    = $request->attributes->get('_format', 'html');
        $templates = [];

        if ($controllerInfo = $this->parseController($controller)) {
            $template = sprintf('%s/%s.%s.twig', $controllerInfo['name'], $controllerInfo['action'], $format);

            if ($this->templateExists($template)) {
                return $template;
            } else {
                $templates[] = $template;
            }
        }

        $route = $this->routes->get($request->attributes->get('_route'));

        if ($route && $route->hasContent()) {
            $template = sprintf('%s/%s.%s.twig', $route->getContent(), $route->isList() ? 'list' : 'show', $format);

            if ($this->templateExists($template)) {
                return $template;
            } else {
                $templates[] = $template;
            }
        }

        return array_pop($templates);
    }