Jarves\Navigation::getRendered PHP Method

getRendered() public method

whether the template cache is deactivated. Navigation object is still cached boolean noCache = false whether the pathInfo is used in the cacheKey instead of the currentUrl. Useful when you have in your navigation controller calls that are based on pathInfo like pageStack->getCurrentUrlAffix() boolean pathInfoCache = false Example: getRendered(['noCache' => true]);
public getRendered ( array $options, Twig_Environment $twig ) : string
$options array
$twig Twig_Environment
return string
    public function getRendered($options, \Twig_Environment $twig)
    {
        $options['noCache'] = isset($options['noCache']) ? (bool) $options['noCache'] : false;
        $options['pathInfoCache'] = isset($options['pathInfoCache']) ? (bool) $options['pathInfoCache'] : false;
        $view = $options['template'] ?: $options['view'];
        $cacheKey = 'core/navigation/' . $this->pageStack->getCurrentPage()->getDomainId() . '.' . $this->pageStack->getCurrentPage()->getId() . ($options['pathInfoCache'] ? '_' . md5($this->pageStack->getRequest()->getPathInfo()) : '') . '_' . md5(json_encode($options));
        $fromCache = false;
        $viewPath = $this->jarves->resolvePath($view, 'Resources/views/');
        if ('@' === $view[0]) {
            $view = substr($view, 1);
        }
        if (!file_exists($viewPath)) {
            throw new \Exception(sprintf('View `%s` not found.', $view));
        } else {
            $mtime = filemtime($viewPath);
        }
        if (!$options['noCache']) {
            $cache = $this->cacher->getDistributedCache($cacheKey);
            if ($cache && isset($cache['html']) && $cache['html'] !== null && $cache['mtime'] == $mtime) {
                return $cache['html'];
            }
        }
        $cache = $this->cacher->getDistributedCache($cacheKey);
        if ($cache && isset($cache['object']) && $cache['mtime'] == $mtime) {
            $navigation = unserialize($cache['object']);
            $fromCache = true;
        } else {
            $navigation = $this->get($options);
        }
        $data['navigation'] = $navigation ?: false;
        if ($navigation !== false) {
            $html = $twig->render($view, $data);
            if (!$options['noCache']) {
                $this->cacher->setDistributedCache($cacheKey, array('mtime' => $mtime, 'html' => $html));
            } elseif (!$fromCache) {
                $this->cacher->setDistributedCache($cacheKey, array('mtime' => $mtime, 'object' => serialize($navigation)));
            }
            return $html;
        }
        //no navigation found, probably the template just uses the breadcrumb
        return $twig->render($view, $data);
    }

Usage Example

Beispiel #1
0
 public function navigationLevel(\Twig_Environment $twig, $level, $view = 'JarvesBundle:Default:navigation.html.twig', array $options = [])
 {
     $options = array_merge($options, ['level' => $level, 'template' => $view]);
     return $this->navigation->getRendered($options, $twig);
 }