forked from GNUsocial/gnu-social
[TWIG] Add twig function to output the active tag if the current route matches a given one
This commit is contained in:
parent
cac00dd6d4
commit
1572261617
54
src/Twig/Extension.php
Normal file
54
src/Twig/Extension.php
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* GNU social Twig extensions
|
||||||
|
*
|
||||||
|
* @package GNUsocial
|
||||||
|
* @category Twig
|
||||||
|
*
|
||||||
|
* @author Hugo Sales <hugo@fc.up.pt>
|
||||||
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Twig;
|
||||||
|
|
||||||
|
use Twig\Extension\AbstractExtension;
|
||||||
|
use Twig\TwigFilter;
|
||||||
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
|
class Extension extends AbstractExtension
|
||||||
|
{
|
||||||
|
public function getFilters()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
// new TwigFilter('foo', [GSRuntime::class, 'foo']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFunctions()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
/** Twig function to output the 'active' class if the current route matches the given route */
|
||||||
|
new TwigFunction('active', [Runtime::class, 'isCurrentRouteActive']),
|
||||||
|
new TwigFunction('is_route', [Runtime::class, 'isCurrentRoute']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
70
src/Twig/Runtime.php
Normal file
70
src/Twig/Runtime.php
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
// {{{ License
|
||||||
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
||||||
|
//
|
||||||
|
// GNU social is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU Affero General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// GNU social is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU Affero General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
// }}}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Common utility functions
|
||||||
|
*
|
||||||
|
* @package GNUsocial
|
||||||
|
* @category Util
|
||||||
|
*
|
||||||
|
* @author Hugo Sales <hugo@fc.up.pt>
|
||||||
|
* @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
|
||||||
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Twig;
|
||||||
|
|
||||||
|
use Functional as F;
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
||||||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
use Twig\Extension\RuntimeExtensionInterface;
|
||||||
|
|
||||||
|
class Runtime implements RuntimeExtensionInterface, EventSubscriberInterface
|
||||||
|
{
|
||||||
|
private Request $request;
|
||||||
|
public function __constructor()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isCurrentRouteActive(string ...$routes): string
|
||||||
|
{
|
||||||
|
return $this->isCurrentRoute('active', ...$routes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isCurrentRoute(string $class, string ...$routes): string
|
||||||
|
{
|
||||||
|
$current_route = $this->request->attributes->get('_route');
|
||||||
|
return F\some($routes, F\equal($current_route)) ? $class : '';
|
||||||
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------
|
||||||
|
|
||||||
|
// Request is not a service, can't find a better way to get it
|
||||||
|
public function onKernelRequest(RequestEvent $event)
|
||||||
|
{
|
||||||
|
$this->request = $event->getRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return [KernelEvents::REQUEST => 'onKernelRequest'];
|
||||||
|
}
|
||||||
|
}
|
@ -12,31 +12,37 @@
|
|||||||
|
|
||||||
{% block body %}
|
{% block body %}
|
||||||
|
|
||||||
<div class='content'>
|
<div class='content'>
|
||||||
<nav class='set-nav'>
|
<nav class='set-nav'>
|
||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('settings_profile') }}" class='hover-effect {% if (app.request.attributes.get('_route') starts with 'settings_profile') or (app.request.attributes.get('_route') starts with 'settings_avatar')%}active{% endif %}'>Settings</a>
|
<a href="{{ path('settings_profile') }}" class='hover-effect {{ active('settings_profile', 'settings_avatar') }}'>Settings</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('settings_account') }}" class='hover-effect {% if app.request.attributes.get('_route') starts with 'settings_account' %}active{% endif %}'>Account</a>
|
<a href="{{ path('settings_account') }}" class='hover-effect {{ active('settings_account') }}'>Account</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('settings_misc') }}" class='hover-effect {% if app.request.attributes.get('_route') starts with 'settings_misc' %}active{% endif %}'>Misc</a>
|
<a href="{{ path('settings_misc') }}" class='hover-effect {{ active('settings_misc') }}'>Misc</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
<nav class='set-nav2'>
|
<nav class='set-nav2'>
|
||||||
<ul>
|
<ul>
|
||||||
|
{# {% for tab in tabs %} #}
|
||||||
|
{# {% endfor %} #}
|
||||||
|
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('settings_profile') }}" class='hover-effect {% if app.request.attributes.get('_route') starts with 'settings_profile' %}active{% endif %}'>Profile</a>
|
<a href="{{ path('settings_profile') }}" class='hover-effect {{ active('settings_profile') }}'>Profile</a>
|
||||||
</li>
|
</li>
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('settings_avatar') }}" class='hover-effect {% if app.request.attributes.get('_route') starts with 'settings_avatar' %}active{% endif %}'>Avatar</a>
|
<a href="{{ path('settings_avatar') }}" class='hover-effect {{ active('settings_avatar') }}'>Avatar</a>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</nav>
|
</nav>
|
||||||
|
</div>
|
||||||
|
|
||||||
{% block form %}
|
{% block form %}
|
||||||
|
|
||||||
<div class='form'>
|
<div class='form'>
|
||||||
{{ form(prof) }}
|
{{ form(prof) }}
|
||||||
</div>
|
</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user