cho*_*ise 34 php url symfony twig
以下简单代码:
<li><a href="{{ path('_list') }}">List</a></li>
Run Code Online (Sandbox Code Playgroud)
class="active"
如果当前页面与_list
路线匹配,是否有一种简单的方法来添加?
使用symfony2和twig的最新PR-Release作为模板引擎
小智 91
Twig允许条件,并且Request对象在整个应用程序中都可用.如果要包含模板,请获取要使用的路径:
app.request.attributes.get('_route')
Run Code Online (Sandbox Code Playgroud)
如果您正在使用渲染功能,则需要使用:
app.request.attributes.get('_internal')
Run Code Online (Sandbox Code Playgroud)
有了它,你应该能够使用:
class="{% if app.request.attributes.get('_route') == '_list' %}active{% endif %}"
Run Code Online (Sandbox Code Playgroud)
或更短:
class="{{ app.request.get('_route') == '_list' ? 'active' }}"
Run Code Online (Sandbox Code Playgroud)
Joh*_*ich 21
有时您不希望对路线进行精确匹配.对于这些情况,您可以使用twigs的"starts with"条件逻辑.
例如,假设您正在使用书籍.您有以下路线: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>
Run Code Online (Sandbox Code Playgroud)
此示例至少适用于Symfony 2.3.x.
Max*_*sky 19
最短版本:
{% set route = app.request.get('_route') %}
<li class="{{ route starts with 'post' ? 'open' }}"></li>
<li class="{{ route starts with 'category' ? 'open' }}"></li>
Run Code Online (Sandbox Code Playgroud)
有时有用:
{% set route = app.request.get('_route') %}
<li class="{{ 'post' in route ? 'open' }}"></li>
<li class="{{ 'category' in route ? 'open' }}"></li>
Run Code Online (Sandbox Code Playgroud)
小智 8
使用三元运算符:
{% 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>
Run Code Online (Sandbox Code Playgroud)
这是用 symfony 3.4 完成的,但可能类似的事情可以用 SF2 完成。
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 '';
}
}
Run Code Online (Sandbox Code Playgroud)
将此添加到 services.yml
services:
#... some other services
AppBundle\Twig\AppExtension:
arguments: ["@request_stack"]
Run Code Online (Sandbox Code Playgroud)
用法:
<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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
41424 次 |
最近记录: |