1

在我的 symfony2 应用程序中,我创建了一个仪表板,该仪表板当前包含许多导航元素。现在我试图将这些元素分成几个包。

这是我的代码:

{# app/Resources/views/base.html.twig #}

{# ... #}
{% block body %} {% endblock %}
{# ... #}

然后在 ProfileBundle 中:

{# src/MyApp/ProfileBundle/Resources/views/Dashboard/index.html.twig #}

{% block body %}
    <p>Heading</p>
    <ul>
    {% block dashboardNavi %} {% endblock %}
    </ul>
{% block %}

编辑:控制器:

class DashboardController extends Controller
{
    public function indexAction()
    {
        return $this->render('MyAppProfileBundle:Dashboard:index.html.twig', array());
    }
}

路由:

pricecalc_profile_dashboad_security:
    pattern: /dashboard
    defaults: {_controller: MyAppProfileBundle:Dashboard:index }

当我的路线“/dashboard”被加载时,该模板被正确呈现。

我现在想做的是dashboardNavi在多个 Bundle 中扩展该 -Block,而不改变我的 ProfileBundle 的路线。这些 Bundle 中的每一个都为自定义操作带来了它自己的路由和控制器,但是所有的 bundle 都应该扩展该块以将其自定义操作的链接添加到仪表板屏幕。

到目前为止,我所拥有的是:

{# src/MyApp/ProfileNewsletterBundle/Resources/views/Dashboard/indexNewsletter.html.twig #}

{% extends 'MyAppProfileBundle:Dashboard:index.html.twig' %}
{% block dashboardNavi %}
    {{ parent() }}
    <li><a href="#">Test</a></li>
{% endblock %}

但该模板永远不会呈现。

编辑 2: 也许我对 symfony 在模板继承方面的工作方式的理解有点错误。我会指定我想要做什么。

我有一个 Bundle ( DashboardBundle),它由自己的路由、控制器、视图等组成。视图包含两个块 -navigationdashboard. 现在,我想让这两个块由其他一些 Bundle 扩展 - 只需在该块上添加新的导航项dashboard和快捷方式。navigation我想在不修改我的 Dashboard-Bundle 的情况下进行这些增强——如果可能的话。

完成后,我将拥有 16 个捆绑包,每个捆绑包都在自己的控制器中提供自己的功能——它们应该只是链接到那个仪表板上。是否可以在不修改视图本身的情况下以这种方式扩展仪表板视图?

4

1 回答 1

0

在了解了 symfony 在扩展控制器和视图方面的工作原理后,我终于设法解决了这个问题。

我添加了一个新的控制器:

{# src/MyApp/ProfileNewsletterBundle/Controllers/DashboardController.php #}
class DashboardController extends Controller {
    public function indexAction()
    {
        return $this->render('ProfileNewsletterBundle:Dashboard:index.html.twig', array());
    }
}

修改包ProfileNewsletterBundle以让方法getParent返回ProfileBundle

并修改了视图:

{% extends 'ProfileBundle:Dashboard:index.html.twig' %}

{% block dashboardNavi %}
    <li><a href="#">Test</a></li>
{% endblock %}

到目前为止,这似乎工作正常。谢谢大家花时间在这上面。

于 2015-05-26T07:49:05.007 回答