php 添加活动类以与 sf2 和 twig 链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5696526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add active class to link with sf2 and twig
提问by choise
following simple code:
以下简单代码:
<li><a href="{{ path('_list') }}">List</a></li>
is there a simple way to add an class="active"
if the current page matches the _list
route?
class="active"
如果当前页面与_list
路线匹配,是否有一种简单的方法可以添加?
using the newest PR-Release of symfony2 and twig as template engine
使用最新的 symfony2 和 twig 作为模板引擎的 PR-Release
回答by Brian
Twig allows for conditionals and the Request object is available throughout the application. If you are including the template, to get the route you want to use:
Twig 允许使用条件,并且 Request 对象在整个应用程序中都可用。如果要包含模板,要获取要使用的路线:
app.request.attributes.get('_route')
If you are using the render function, you want to use:
如果您正在使用渲染功能,您要使用:
app.request.attributes.get('_internal')
With that, you should be able to use:
有了这个,你应该能够使用:
class="{% if app.request.attributes.get('_route') == '_list' %}active{% endif %}"
or shorter:
或更短:
class="{{ app.request.get('_route') == '_list' ? 'active' }}"
回答by John Kramlich
Sometimes you don't want to do exact matching of a route. For those cases, you can use the "starts with" conditional logic of twig.
有时您不想对路线进行精确匹配。对于这些情况,您可以使用 twig 的“开始于”条件逻辑。
As an example, lets assume you are working with books. You have the following routes: book, book_show, book_new, book_edit. You want the navigation item Book to be highlighted for any of those cases. This code would accomplish that.
例如,假设您正在处理书籍。您有以下路线:book、book_show、book_new、book_edit。您希望针对任何这些情况突出显示导航项 Book。这段代码将实现这一点。
<a class="{% if app.request.attributes.get('_route') starts with 'book' %}active{% endif %}">Books</a>
<a class="{% if app.request.attributes.get('_route') starts with 'author' %}active{% endif %}">Authors</a>
This example works with at least Symfony 2.3.x
此示例至少适用于 Symfony 2.3.x
回答by Max Lipsky
Shortest version:
最短版本:
{% set route = app.request.get('_route') %}
<li class="{{ route starts with 'post' ? 'open' }}"></li>
<li class="{{ route starts with 'category' ? 'open' }}"></li>
Sometimes useful:
有时有用:
{% set route = app.request.get('_route') %}
<li class="{{ 'post' in route ? 'open' }}"></li>
<li class="{{ 'category' in route ? 'open' }}"></li>
回答by Yuriy Korman
With ternary operator:
使用三元运算符:
{% set route = app.request.attributes.get('_route') %}
<ul class="nav navbar-nav">
<li {{ route == 'profile_index' ? 'class="active"' }}><a href="{{ path('profile_index') }}"><i class="icon-profile position-left"></i> My Profile</a></li>
<li {{ route == 'influencers_index' ? 'class="active"'}}><a href="{{ path('influencers_index') }}"><i class="icon-crown position-left"></i> Influencers</a></li>
<li {{ route == 'task_manager_index' ? 'class="active"'}}><a href="{{ path('task_manager_index') }}"><i class="icon-alarm-check position-left"></i> Task Manager</a></li>
</ul>
回答by Darius.V
This is done with symfony 3.4, but probably something similar can be done with SF2.
这是用 symfony 3.4 完成的,但可能类似的事情可以用 SF2 完成。
src\AppBundle\Twig\AppExtension.php
src\AppBundle\Twig\AppExtension.php
<?php
namespace AppBundle\Twig;
use Symfony\Component\HttpFoundation\RequestStack;
class AppExtension extends \Twig_Extension
{
private $requestStack;
public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('activeMenu', [$this, 'activeMenu'])
];
}
/**
* Pass route names. If one of route names matches current route, this function returns
* 'active'
* @param array $routesToCheck
* @return string
*/
public function activeMenu(array $routesToCheck)
{
$currentRoute = $this->requestStack->getCurrentRequest()->get('_route');
foreach ($routesToCheck as $routeToCheck) {
if ($routeToCheck == $currentRoute) {
return 'active';
}
}
return '';
}
}
Add this to services.yml
将此添加到 services.yml
services:
#... some other services
AppBundle\Twig\AppExtension:
arguments: ["@request_stack"]
Usage:
用法:
<ul class="nav navbar-nav">
<li class="{{ activeMenu(['form', 'edit_form']) }}"><a href="{{ path('form') }}">Form</a></li>
<li class="{{ activeMenu(['list']) }}"><a href="{{ path('list') }}">List</a></li>
</ul>
回答by choise
i found a very good Bundle that handles all this stuff automagically:
我找到了一个非常好的 Bundle,可以自动处理所有这些东西:
回答by Karol Gontarski
SF2.2
SF2.2
{{ dump(app.request.server.get('PATH_INFO')) }}
回答by Degas
Symfony2.3, in Twig, try this to get uri:
Symfony2.3,在 Twig 中,试试这个来获取 uri:
{{ dump(app.request.server.get("REQUEST_URI")) }}
回答by Baig
This is how I do it (using Symfony 2.6)
这就是我的做法(使用 Symfony 2.6)
<li {% if app.request.get('_route') == '_homepage' %} class="active" {% endif %}><a href="{{ path('_homepage') }}">Student</a></li>
'_homepage'
is the name of route in routing.yml
of your bundle and the route looks like this
'_homepage'
是routing.yml
您的捆绑包中的路线名称,路线如下所示
_homepage:
path: /
defaults: { _controller: CoreBundle:Default:index }
回答by Marcin ???a
class="class_name {% if loop.index0 == 0 %}CLASSNAME{% endif %}"
class="class_name {% if loop.index0 == 0 %}CLASSNAME{% endif %}"