我想在opencart上手动编写商店的侧边菜单,我有一个问题 - 如何使twig将类“active”添加到当前页面的链接
我试着这样做
<a href="/something/" class="list-group-item {{ (app.request.attributes.get('_route') == 'something') ? 'active' }}">page about something</a>
但它不起作用
这正是类别模块的功能,因此您可以复制它:
{% if child.category_id == child_id %}
<a href="{{ child.href }}" class="list-group-item active"> - {{ child.name }}</a>
{% else %}
<a href="{{ child.href }}" class="list-group-item"> - {{ child.name }}</a>
{% endif %}
你可以在这里找到上面的文件:
/catalog/view/theme/default/template/extension/module/category.twig
你可以在这里找到它的控制器:
/catalog/controller/extension/module/category.php
他们这样做的方法是通过控制器电话文件输出路径请求。这将是如下所示:
$urlroute = $this->request->get['route'];
然后在twig文件中,您只需检查IF查询中的路径变量即可
{% if urlroute == "something" % }
<a href="/something/" class="list-group-item active">page about something</a>
{% else %}
<a href="/something/" class="list-group-item">page about something</a>
{% endif %}