1
votes

I am new to symfony2 so please forgive me if this is a stupid question.

Symfony2 is organised in bundles - so everything is a bindle right?

Based on this I have created the following bundles in order to have a simple login mechanism:

  • App
    • The main bundle will contain all global functionality
  • User
    • Will be used to represent users

So the bundles work correctly and all is fine.

Now I cant figure out the best way to add a layout/theme structure to the site.

I obviously need some global assets such as header, nav and foooter. But additionally, there needs to be some global css style sheets, jquery etc.

The most obvious place bundle is App - but how do i make all other bundles inherit the theme from this bundle. For example, the user bundle template needs to extend the App bundle etc.

The idea for bundles is that they are modular and self contained, therefore how can this be achieved

3
You can access any other bundles templates etc, by their bundle name. Think of them like a class, they are contained, but accessible.Seer

3 Answers

1
votes

I personally use like this:

There is MainBundle (in your case, it is App) which does global services, twig extensions and layout. Global assets are included in this file.

Main layout of all the other bundles extends the layout of MainBundle. Templates inside each bundle extend to the main layout of it which extend the layout of MainBundle. For example,

- MainBundle
    - views
         - layout.html.twig
- UserBundle
    - views
         - layout.html.twig (extends to MainBundle/layout)
         - show.html.twig (extends to UserBundle/layout)
         - friends.html.twig (extends to UserBundle/layout)
1
votes

Everything is explained in the official docs: http://symfony.com/doc/current/templating.html

The basic global view (templates) resources are placed in the following dir:

app/Resources/views/index.html.twig

If you have a specific purpose, or bundles, templates, place them in the subdirs, e.g.:

app/Resources/views/blog/index.html.twig

And if you want to keep everything in the bundle (must for reusable code), use this convention:

[VendorName/]YourBundle/Resources/views/Blog/index.html.twig

(Of course, any name, except the ".html.twig" extension for Twig, may be changed to your liking)

0
votes

The strategy I prefer is to organize my application in a single bundle. If you have no intention of re-using distinct standalone features across multiple applications then this is the most appropriate way. Having a "UserBundle" in your own application namespace probably doesn't make sense. You're adding a lot of extra structure that's serving you no advantage. Consider this instead:

- MainBundle
    - Controller
        - UserController
        - OtherController
    - Resources
        - views
            - layout.html.twig
            - User
                - show.html.twig
                - update.html.twig
                - friends.html.twig
            - Other
                - some_other_view.html.twig

In this case the templates under the controller directories would extend MainBundle::layout.html.twig.