Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #98 from sebastianljunggren/master

Add option currentScanDepth to renderers
  • Loading branch information...
commit 9f4235fb2ea6d173dc277e11821c1a1fc38bf308 2 parents 52c8ffd + bafe93c
Maxime Veber Nek- authored
2  doc/01-Basic-Menus.markdown
View
@@ -230,6 +230,8 @@ Most renderers also support several other options, which can be passed as
the second argument to the `render()` method:
* `depth`
+* `matchingDepth`: The depth of the scan to determine whether an item
+ is an ancestor of the current item.
* `currentAsLink` (default: `true`)
* `currentClass` (default: `current`)
* `ancestorClass` (default: `current_ancestor`)
7 src/Knp/Menu/Renderer/ListRenderer.php
View
@@ -23,6 +23,7 @@ public function __construct(MatcherInterface $matcher, array $defaultOptions = a
$this->matcher = $matcher;
$this->defaultOptions = array_merge(array(
'depth' => null,
+ 'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
@@ -88,6 +89,10 @@ protected function renderChildren(ItemInterface $item, array $options)
$options['depth'] = $options['depth'] - 1;
}
+ if (null !== $options['matchingDepth'] && $options['matchingDepth'] > 0) {
+ $options['matchingDepth'] = $options['matchingDepth'] - 1;
+ }
+
$html = '';
foreach ($item->getChildren() as $child) {
$html .= $this->renderItem($child, $options);
@@ -119,7 +124,7 @@ protected function renderItem(ItemInterface $item, array $options)
if ($this->matcher->isCurrent($item)) {
$class[] = $options['currentClass'];
- } elseif ($this->matcher->isAncestor($item, $options['depth'])) {
+ } elseif ($this->matcher->isAncestor($item, $options['matchingDepth'])) {
$class[] = $options['ancestorClass'];
}
1  src/Knp/Menu/Renderer/TwigRenderer.php
View
@@ -26,6 +26,7 @@ public function __construct(\Twig_Environment $environment, $template, MatcherIn
$this->matcher = $matcher;
$this->defaultOptions = array_merge(array(
'depth' => null,
+ 'matchingDepth' => null,
'currentAsLink' => true,
'currentClass' => 'current',
'ancestorClass' => 'current_ancestor',
8 src/Knp/Menu/Resources/views/knp_menu.html.twig
View
@@ -32,7 +32,11 @@
{% set currentItem = item %}
{# update the depth for children #}
{% if options.depth is not none %}
-{% set options = currentOptions|merge({'depth': currentOptions.depth - 1}) %}
+{% set options = options|merge({'depth': currentOptions.depth - 1}) %}
+{% endif %}
+{# update the matchingDepth for children #}
+{% if options.matchingDepth is not none and options.matchingDepth > 0 %}
+{% set options = options|merge({'matchingDepth': currentOptions.matchingDepth - 1}) %}
{% endif %}
{% for item in currentItem.children %}
{{ block('item') }}
@@ -48,7 +52,7 @@
{%- set classes = item.attribute('class') is not empty ? [item.attribute('class')] : [] %}
{%- if matcher.isCurrent(item) %}
{%- set classes = classes|merge([options.currentClass]) %}
- {%- elseif matcher.isAncestor(item, options.depth) %}
+ {%- elseif matcher.isAncestor(item, options.matchingDepth) %}
{%- set classes = classes|merge([options.ancestorClass]) %}
{%- endif %}
{%- if item.actsLikeFirst %}
21 tests/Knp/Menu/Tests/Renderer/AbstractRendererTest.php
View
@@ -291,4 +291,25 @@ public function testEmptyUncompressed()
$rendered = '';
$this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 0, 'compressed' => false)));
}
+
+ public function testMatchingDepth0()
+ {
+ $this->menu['Parent 1']['Child 1']->setCurrent(true);
+ $rendered = '<ul class="root"><li class="first"><span>Parent 1</span></li><li class="last"><span>Parent 2</span></li></ul>';
+ $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 1,'matchingDepth' => 1)));
+ }
+
+ public function testMatchingDepth1()
+ {
+ $this->menu['Parent 1']['Child 1']->setCurrent(true);
+ $rendered = '<ul class="root"><li class="current_ancestor first"><span>Parent 1</span></li><li class="last"><span>Parent 2</span></li></ul>';
+ $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 1,'matchingDepth' => 2)));
+ }
+
+ public function testMatchingDepth2()
+ {
+ $this->menu['Parent 1']['Child 1']->setCurrent(true);
+ $rendered = '<ul class="root"><li class="first"><span>Parent 1</span></li><li class="last"><span>Parent 2</span></li></ul>';
+ $this->assertEquals($rendered, $this->renderer->render($this->menu, array('depth' => 1,'matchingDepth' => 0)));
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.