简体   繁体   English

通过扩展树枝模板创建hedear菜单

[英]Creating a hedear menu by extending twig templates

I'm trying to achieve menu below but for some reason it doesn't show middle parts (level 2 and 3) of the menu if I navigate up to level 3 and 4. If I'm on level 3 then 1, 2, 3 should be visible. 我正在尝试实现下面的菜单,但是由于某些原因,如果我导航到3和4级,则菜单的中间部分(2和3级)不会显示。如果我位于3级,则显示1、2 3应该是可见的。 If I'm on level 4 then all the levels. 如果我在4级,那么所有级别。 That's what I want to achieve. 这就是我要实现的目标。

I read whole templating documentation , this post and some more but cannot find why my code below won't work. 我阅读了整个模板文档这篇文章以及更多内容,但找不到下面的代码为什么行不通的原因。

Expected: 预期:

FRONTEND - BACKEND
----------------------
COUNTRY | LEAGUE      -> After selecting FRONTEND in level 1 above
----------------------
INDEX | LIST | CREATE -> After selecting COUNTRY in level 2 above
----------------------
Countries will appear here after selecting LIST in level 3 above

My failed attempts: 我失败的尝试:

FRONTEND - BACKEND
----------------------
INDEX | LIST | CREATE -> After selecting COUNTRY in level 2 above
----------------------

or 要么

FRONTEND - BACKEND
----------------------
Countries will appear here after selecting LIST in level 3 above

base.html.twig base.html.twig

   Football
      BackendBundle
      .....
      FrontendBundle
         Resources
            views
               Default
                  index.html.twig
               Country
                  index.html.twig
                  list.html.twig

base.html.twig base.html.twig

<body>
    <a href="{{ path('football_frontend_default_index') }}">FRONTEND</a>
    &nbsp;&dash;&nbsp;
    <a href="{{ path('football_backend_default_index') }}">BACKEND</a>
    <hr />
    {% block body %}{% endblock %}
    {% block javascripts %}{% endblock %}
</body>

Default/index.html.twig 默认/ index.html.twig

{% extends '::base.html.twig' %}

{% block body %}
    {% spaceless %}
        <a href="{{ path('football_frontend_country_index') }}">COUNTRY</a>
        &nbsp;|&nbsp;
        <a href="{{ path('football_frontend_league_index') }}">LEAGUE</a>
        <hr />
    {% endspaceless %}
{% endblock %}

Country/inedx.html.twig 国家/ inedx.html.twig

{% extends 'FootballFrontendBundle:Default:index.html.twig' %}

{% block body %}
    {% spaceless %}
        <a href="{{ path('football_frontend_country_index') }}">Index</a>
        &nbsp;|&nbsp;
        <a href="{{ path('football_frontend_country_list') }}">List</a>
        &nbsp;|&nbsp;
        <a href="{{ path('football_frontend_country_create') }}">create</a>
        <hr />
    {% endspaceless %}
{% endblock %}

Country/list.html.twig 国家/ list.html.twig

{% extends 'FootballFrontendBundle:Country:index.html.twig' %}

{% block body %}
    {% spaceless %}
        COUNTRY - List
        <hr />
        ....
    {% endspaceless %}
{% endblock %}

When you use the body block in your file Country/list.html.twig , you replace the one in the root file base.html.twig . 当您在文件Country/list.html.twig使用body块时,将替换根文件base.html.twig中的Country/list.html.twig块。 It's the same than method inheritance in PHP. 它与PHP中的方法继承相同。 If you want to do what you try to achieve, you have two ways : 如果您想做自己想做的事情,则有两种方法:

  1. Use {{ parent() }} inside your block, to display the parent template content of the block 在您的区块内使用{{parent()}},以显示区块的父模板内容
  2. Use a different name for your blocks for each subtemplate 为每个子模板的块使用不同的名称

Eg 例如

Default/index.html.twig 默认/ index.html.twig

{% extends '::base.html.twig' %}

{% block body %}
    {% spaceless %}
        <a href="{{ path('football_frontend_country_index') }}">COUNTRY</a>
        &nbsp;|&nbsp;
        <a href="{{ path('football_frontend_league_index') }}">LEAGUE</a>
        <hr />
    {% endspaceless %}
    {% block body2 %}{% endblock %}
{% endblock %}

Country/inedx.html.twig 国家/ inedx.html.twig

{% extends 'FootballFrontendBundle:Default:index.html.twig' %}

{% block body2 %}
    {% spaceless %}
        <a href="{{ path('football_frontend_country_index') }}">Index</a>
        &nbsp;|&nbsp;
        <a href="{{ path('football_frontend_country_list') }}">List</a>
        &nbsp;|&nbsp;
        <a href="{{ path('football_frontend_country_create') }}">create</a>
        <hr />
    {% endspaceless %}
    {% block body3 %}{% endblock %}
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM