Search code examples
symfony1

The best way to include footer, header and side menu in Symfony


Currently in my symfony app I created a module header and then a header component to handle my header section in templates, same for footer,

Is this the best way to go?

Also I am a bit unsure what to do with the side menu that is included in some sections, should I create a components side-menu of my categories module and use the slot thing to get this going?


Solution

  • Partials are probably the best way to go.

    But slots are really useful if what you want to drop into the layout is only present for some actions and not all.

    You said you have created a module for both header and footer. I prefer to just create a partial in the application templates folder for each of these.

    /apps
      /yourapp
        /templates
          _header.php
          _footer.php
          layout.php
    

    The categories sidebar sounds like a component, but how you implement it depends whether you want the same component shown on every page or be able to control what sidebar is shown (if at all) on different pages.

    If you want the same component on every page just include the component (with include_component) within your layout.php file. If you want more control, include the component from with the template of each of the actions you require it to be shown.

    A more complex solution for the sidebar would be include it from your layout if a particular attribute is set. So in you actions.class.php:

    public function preExecute()
    {
      $this->getRequest()->setAttribute('show_categories_sidebar', true);
    }
    

    Then in you layout.php:

    <?php if ($sf_request->hasAttribute('show_categories_sidebar')): ?>
      <div id="sidebar">
        <?php include_component('category', 'sidebar') ?>
      </div>
    <?php endif; ?>