Active Link
, (*1)
A very simple Silex service provider for checking active links in Twig., (*2)
Example
In PHP:, (*3)
use Herrera\Silex\ActiveLinkServiceProvider;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpFoundation\Request;
$app = new Application();
$app->get(
'/',
function (Application $app) {
return $app['twig']->render('test.html.twig');
}
)->bind('home');
$app->get(
'/page',
function (Application $app) {
return $app['twig']->render('test.html.twig');
}
)->bind('page');
$app->register(
new TwigServiceProvider(),
array(
'twig.path' => '/path/to/templates'
)
);
$app->register(
new ActiveLinkServiceProvider()
);
$app->run(
Request::create('/page')
);
The Twig template:, (*4)
<ul>
<li{{ active("home") }}><a href="{{ path("home") }}">Home</a></li>
<li{{ active("page") }}><a href="{{ path("page") }}">Page</a></li>
</ul>
The result when request page
:, (*5)
<ul>
<li><a href="/">Home</a></li>
<li class="active"><a href="/page">Page</a></li>
</ul>
Installation
Use Composer:, (*6)
$ composer require "herrera-io/silex-active-link=~1.0"
Configuration
There is only one configuration parameter: active_link.snippet
, (*7)
The active_link.snippet
is the result returned if a link is active. By
default, the result is class="active"
(note that the space is included).
Hopefully, this provides you with a far greater degree of flexibility in
how you can use the new active
Twig function., (*8)