Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update from fork #1

Merged
merged 8 commits into from
Jan 7, 2020
116 changes: 64 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,83 +1,95 @@
ActiveMenuItemBundle
====================

The active menu item highlight of simple HTML menu for Symfony framework

## How to install
Pretty simple with `Composer`, add to `composer.json` file:

{
"require": {
"bw/active-menu-item-bundle": "~1.1"
}
}
Install it with `Composer` first:

```bash
composer require bw/active-menu-item-bundle
```

and run `$ composer update` command.
Then, to use filters and functions in Twig, register this bundle in `bundles.php`:

### How to include
To use filters and functions in `Twig`, register `BWActiveMenuItemBundle`
in `app/AppKernel.php`:
```php
// config/bundles.php

public function registerBundles()
{
$bundles = array(
// other bundles...
new BW\ActiveMenuItemBundle\BWActiveMenuItemBundle(),
);
return [
// other bundles...
BW\ActiveMenuItemBundle\BWActiveMenuItemBundle::class => ['all' => true],
};
```

## How to use
To check whether menu item route is active, simply use `is_active` twig filter:

{{ path('route_name')|is_active }}
To check whether menu item route is active, simply use `is_active` Twig filter:

```twig
{{ 'route_name'|is_active }}
````

or use `is_active_uri` twig filter for check whether menu item request URI is active:
Or use `is_active_uri` Twig filter for check if menu item's request URI is active:

{{ 'route_name'|is_active_uri }}
```twig
{{ path('route_name')|is_active_uri }}
```

If route or request URI is match, filters return `current active` string.

### How to use with multilevel menu
To check array of possible routes use `is_active` function,
passed array of routes (all submenu item routes of current item)
as a first parameter and the current item route as a second parameter.

To check array of possible routes use `is_active()` Twig function,
passed array of routes (all submenu item routes of current item)
as a first parameter and the current item route as a second parameter.

For example, there are simple HTML menu with submenu:

<ul>
<li class="{{ is_active([
'subcategory1',
'subcategory2',
], 'all_categories') }}">
<a href="{{ path('all_categories') }}">All categories</a>
<ul>
<li class="{{ 'subcategory1'|is_active }}">
<a href="{{ path('subcategory1') }}">Subcategory 1</a>
</li>
<li class="{{ 'subcategory2'|is_active }}">
<a href="{{ path('subcategory2') }}">Subcategory 2</a>
</li>
</ul>
</li>
</ul>

If the current route is `subcategory1`, it has `current active` class
and them parent item with `all_categories` route has only `active` class.

And same for request URIs with `is_active_uri` function.
```html
<ul>
<li class="{{ is_active([
'subcategory1_route_name',
'subcategory2_route_name',
], 'all_categories') }}">
<a href="{{ path('all_categories') }}">All categories</a>
<ul>
<li class="{{ 'subcategory1_route_name'|is_active }}">
<a href="{{ path('subcategory1_route_name') }}">Subcategory 1</a>
</li>
<li class="{{ 'subcategory2_route_name'|is_active }}">
<a href="{{ path('subcategory2_route_name') }}">Subcategory 2</a>
</li>
</ul>
</li>
</ul>
```

If the current route is `subcategory1_route_name`, it will have `current active` class
and its parent item - `all_categories` route - will have only `active` class.

And same for request URIs with `is_active_uri()` Twig function, but instead of passing
an array of routes, pass the array of URIs.

## Demo page

Bundle has demo page with example of simple multilevel HTML menu.
Add to `app/config/routing_dev.yml` file:
```yaml
# config/routes/dev/bw_active_menu.yaml

bw_active_menu_item:
resource: "@BWActiveMenuItemBundle/Resources/config/routing.yml"
prefix: /bw/demo/active-menu-item

Run built in server `php app/console server:run` and demo page will be available
at `http://localhost:8000/bw/demo/active-menu-item/index`.
bw_active_menu_item:
resource: "@BWActiveMenuItemBundle/Resources/config/routing.yml"
prefix: /bw/demo/active-menu-item
```

Run built in server `symfony serve` (or `php bin/console server:run`) and demo page
will be available at http://localhost:8000/bw/demo/active-menu-item/index.

## Under the hood
There are only [few][1] additional simple `Twig` filters and functions for use.

There are only [a few][1] additional simple Twig filters and functions for use.

*It's simple, isn't it? :)*


[1]: https://github.com/bocharsky-bw/ActiveMenuItemBundle/blob/master/src/BW/ActiveMenuItemBundle/Twig/BWExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('bw_active_menu_item');

// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$treeBuilder = new TreeBuilder('bw_active_menu_item');

return $treeBuilder;
}
Expand Down
2 changes: 1 addition & 1 deletion src/BW/ActiveMenuItemBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ services:

bw_active_menu_item.twig.bw_extension:
class: BW\ActiveMenuItemBundle\Twig\BWExtension
arguments: []
arguments: ['@request_stack']
tags:
- { name: twig.extension }
7 changes: 5 additions & 2 deletions src/BW/ActiveMenuItemBundle/Twig/BWExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BW\ActiveMenuItemBundle\Twig;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class BWExtension extends \Twig_Extension
{
Expand All @@ -13,10 +14,12 @@ class BWExtension extends \Twig_Extension

/**
* Constructor
*
* @param RequestStack $requestStack
*/
public function __construct()
public function __construct(RequestStack $requestStack)
{
$this->request = Request::createFromGlobals();
$this->request = $requestStack->getCurrentRequest();
}


Expand Down