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 c开发者_如何学JAVAomponents side-menu of my categories module and use the slot thing to get this going?
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; ?>
I typically put header and footer partials or components in the default module. I then include them in the layouts via slots, giving other actions the ability to override or disable them. So a simplified layout might look like this:
<body>
<?php if (has_slot('header')): ?>
<?php include_slot('header') ?>
<?php else: ?>
<?php include_component('default', 'header') ?>
<?php endif ?>
<?php echo $sf_content ?>
<?php if (has_slot('footer')): ?>
<?php include_slot('footer') ?>
<?php else: ?>
<?php include_partial('default/footer') ?>
<?php endif ?>
</body>
You now have the ability to override the header/footer on a per action/template basis, as well as disable them entirely by setting the slot to false. A similar principal could be used for a side menu.
精彩评论