Search code examples
symfonytwigsymfony-2.6

Permanently extend symfony twig template


In my symfony2 application I created a dashboard which currently consists of many navigation elements. Now I am trying to split those elements into several bundles.

This is the code I have:

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

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

Then in the ProfileBundle:

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

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

edit: The controller:

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

The routing:

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

That template is rendered correctly, when my route "/dashboard" is loaded.

What I now'd like to do, is extend that dashboardNavi-Block in multiple Bundles without changing the route from my ProfileBundle. Each of those Bundles brings it`s own routes and controllers for custom actions, but all bundles should extend that one block to add links for their custom actions to the dashboard screen.

What I have so far is:

{# 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 %}

but that template is never rendered.

edit 2: Maybe my understanding of how symfony is working in terms of template inheritance is kind of wrong. I'll specify what I am trying to do.

I got one Bundle (DashboardBundle) which consists of an own route, controller, view etc. The view contains two blocks - like navigation and dashboard. Now, I would like to have those two blocks extended by some other Bundles - just adding new navigation items and shortcuts on that dashboard and navigation block. I would like to do those enhancements without modifying my Dashboard-Bundle - if that is possible at all.

When finished, I will have 16 Bundles, each providing own functionality in own Controllers - and they should just be linked on that dashboard. Is it possible to have the dashboard-view extended that way without modifying the view itself?


Solution

  • I finally managed to fix that after having understood how symfony works in extending controllers and views.

    I added a new Controller:

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

    modified the bundle ProfileNewsletterBundle to let the method getParent return ProfileBundle,

    and modified the view:

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

    That seems to work fine so far. Thank you all for spending your time on that.