richardhj / contao-knp-menu
用于 knp-menu 的包装扩展。
1.3.1
2024-01-04 20:14 UTC
Requires
- php: >=7.4
- contao/core-bundle: ^4.9 || ^5.0
- knplabs/knp-menu-bundle: ^2.2. || ^3.0
- symfony/config: ^4.4 || ^5.0 || ^6.0
- symfony/http-kernel: ^4.4 || ^5.0 || ^6.0
Requires (Dev)
- contao/easy-coding-standard: ^4.0
- contao/manager-plugin: ^2.7
README
提供 Contao 导航模块,作为 KnpMenu 的实例。
此扩展稳定且支持 Contao >=4.9,并将最终集成到 Contao 核心中,请参阅 contao/contao#3838。
安装
通过 Composer
$ composer require richardhj/contao-knp-menu
用法
通过别名检索导航模块
对于 Contao 后端中的任何导航模块,您都可以定义一个别名。现在您可以通过别名检索此导航
{% set menu = knp_menu_get('main_navigation') %}
您还可以覆盖导航模块中的一些设置
{% set menu = knp_menu_get('main_navigation', [], { 'showHidden': true, 'showProtected': true }) %}
使用默认导航
您不一定需要创建导航模块。只需使用特殊键 'contao'
并在需要时配置第三个参数即可。
{% set menu = knp_menu_get('contao', [], { 'defineRoot': 1 }) %}
在 twig 模板中渲染菜单
此扩展创建用于渲染导航的最大灵活性,因此扩展不提供基础模板。以下是在 twig 模板中检索和解析简单导航的示例。
<div class="flex items-center justify-between py-2"> {% set menu = knp_menu_get('main_navigation') %} <nav itemscope itemtype="http://schema.org/SiteNavigationElement"> {% block navigation %} {% import '@KnpMenu/menu.html.twig' as knp_menu %} <div class="flex"> {% for item in menu.children|filter(v => v.isDisplayed) %} {% if item.current %} <strong class="inline-block font-bold text-blue-800 px-3 py-2 leading-5" aria-current="page"> <span {{ knp_menu.attributes(item.labelAttributes) }}>{{ item.label }}</span> </strong> {% else %} <a href="{{ item.uri }}" {{ knp_menu.attributes(item.linkAttributes) }} class="inline-block px-3 py-2 font-normal leading-5 text-gray-500 hover:text-gray-700 focus:outline-none focus:text-blue-400 transition duration-150 ease-in-out" itemprop="url"> <span itemprop="name" {{ knp_menu.attributes(item.labelAttributes) }}>{{ item.label }}</span> </a> {% endif %} {% endfor %} </div> {% endblock %} </nav> </div>
开发备注
代码风格
vendor/bin/ecs check --fix