tags:

views:

327

answers:

2

What is the best way to create regions in your layout similar to Wordpress's Widgets or Drupal Blocks? What is the best practice method of doing that in CakePHP?

+2  A: 

If by regions you mean a special "content container" (never used WP/Drupal), then it's very easy.

There are several ways to accomplish this, but the one that came to my mind first was this:

  1. Create a helper (or an entire plugin) to handle the "which content goes into which container" logic. Shouldn't be too hard to do because you have many Cake utility classes to help you out with that (such as the Configure class). This should obviously be configurable by the end user.
  2. Create containers in your layout, example:

    <div class="content-container" id="content-container-left">
        <?php echo $yourHelper->outputContent("left"); ?>
    </div>
    
  3. Two options:

    • Content should be based on elements; or
    • Content should be based on custom plugins (which actually do their stuff and output the content)

Note: There are probably better ways to accomplish what you want, this is just the first that came to my mind. I'd recommend some pencil-and-paper planning before you actually code anything, it will improve your chances of finding the best way for your app.

dr Hannibal Lecter
A: 

Hi,

I created a Sidebar Helper recently that you might find useful.

You define the content of the boxes in Cake elements, and then add them by calling ...

$sidebar->addBox(array('element'=>'my_sidebox_element');

... this would render the content of views/elements/my_sidebox_element

Alternatively you can specify te content of a box 'inline':

$sidebar->startBox(array('title' => 'My Inline Box'));
<p>blah <b>blah</b> <span>blah</span></p>
$sidebar->endBox();

The in your layout file call

echo $sidebar->getSidebar();

... and each of your boxes will be rendered as divs

Technically speaking this doesn't need to be used as a 'SideBar' - it ultimately depends on how you render the layout with CSS.

See the documented code for more details:

SidebarHelper on GitHub

ebotunes