/**
     * Renders menu tree. Internal method.
     *
     * @param MenuItem  $item        Menu item
     * @param integer $depth         The depth of children to render
     * @param boolean $renderAsChild Render with attributes on the li (true) or the ul around the children (false)
     *
     * @return string
     */
    protected function doRender(MenuItem $item, $depth = null, $renderAsChild = false)
    {
        /**
         * Return an empty string if any of the following are true:
         *   a) The menu has no children eligible to be displayed
         *   b) The depth is 0
         *   c) This menu item has been explicitly set to hide its children
         */
        if (!$item->hasChildren() || $depth === 0 || !$item->getShowChildren()) {
            return '';
        }

        if ($renderAsChild) {
            $attributes = array('class' => 'menu_level_'.$item->getLevel());
        }
        else {
            $attributes = $item->getAttributes();
        }

        // render children with a depth - 1
        $childDepth = ($depth === null) ? null : ($depth - 1);

        $html = $this->format('<ul'.$this->renderHtmlAttributes($attributes).'>', 'ul', $item->getLevel());
        $html .= $this->renderChildren($item, $childDepth);
        $html .= $this->format('</ul>', 'ul', $item->getLevel());

        return $html;
    }
Example #2
0
    /**
     * Renders menu tree. Internal method.
     *
     * @param MenuItem  $item        Menu item
     * @param integer $depth (optional)
     * @param string $template       The template name
     *
     * @return string
     */
    public function doRender(MenuItem $item, $depth = null, $template = null)
    {
        /**
         * Return an empty string if any of the following are true:
         *   a) The menu has no children eligible to be displayed
         *   b) The depth is 0
         *   c) This menu item has been explicitly set to hide its children
         */
        if (!$item->hasChildren() || $depth === 0 || !$item->getShowChildren()) {
            return '';
        }

        if (null === $template) {
            $template = 'KnpMenuBundle:Menu:menu.html.twig';
        }


        return trim($this->container->get('templating')->render($template, array(
            'item'  => $item,
            'menu' => $this,
            'depth' => $depth,
        )));
    }