Hi,
I have been struggeling quite a lot (I'm a newbie) to add a sidebar to all pages of the site that contains the same widgets.
The requirements are:
1) Have one place in the CMS to add widgets
2) Show those widgets on all pages of pagetype 'Page' or anything that inherits from it.
Now - I have got it working, but I was wondering if there is a better way of doing it. This is what I did:
Created a 'SidebarPage' page type that allows the editors to add the widgets:
--------------------------------------------
class SidebarPage extends Page{
static $has_one = array(
"Sidebar" => "WidgetArea",
);
static $defaults = array(
'ShowInMenus' => 0,
'ShowInSearch' => 0
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab("Root.Content.Widgets", new WidgetAreaEditor("Sidebar"));
return $fields;
}
}
class SidebarPage_Controller extends Page_Controller{
}
--------------------------------------------
Added a SidebarPage.ss:
--------------------------------------------
$Sidebar
--------------------------------------------
And then in mysite/code/page.php - page_controller I added:
--------------------------------------------
function getSidebar(){
$sidebarPage = DataObject::get_one('SidebarPage');
return ($sidebarPage) ? $sidebarPage->renderWith("SidebarPage") : false;
}
--------------------------------------------
Now in Page.ss (or any other template), I can render the sidebarwidgets by simply adding $getSideBar in the content.
Especially the 'renderWith' part took ages for me to find out. Is there a simpler or better way to do this? Or am I spot on?
Thanks,
Mike.