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

时间:2015-05-17 11:54:33

标签: symfony twig

我正在尝试实现下面的菜单,但由于某种原因,如果我导航到3级和4级,它不显示菜单的中间部分(2级和3级)。如果我在3级然后1 ,2,3应该是可见的。如果我在4级,那么所有级别。这就是我想要实现的目标。

我阅读了整个templating documentationthis post以及更多内容,但无法找到为什么我的代码无效。

预期:

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

我失败的尝试:

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

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

base.html.twig

   Football
      BackendBundle
      .....
      FrontendBundle
         Resources
            views
               Default
                  index.html.twig
               Country
                  index.html.twig
                  list.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>

默认/ 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 %}

国家/ 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 %}

国家/ list.html.twig

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

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

1 个答案:

答案 0 :(得分:1)

当您在文件body中使用Country/list.html.twig块时,替换根文件base.html.twig中的块。它与PHP中的方法继承相同。如果你想做你想做的事,你有两种方法:

  1. 在块中使用{{parent()}},以显示块的父模板内容
  2. 为每个子模板使用不同的名称
  3. E.g。

    默认/ 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 %}
    

    <强>国家/ 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 %}