Snippets - Little bits of code to make you happy
Check to see if a widget area has any widgets
Tweet5 April 2009 | |
If you want to check that a WidgetArea actually has widgets, and only display it if it has, you can add this function to your Page_Controller (where widgetBarID is the name of your widgetArea + ID)
function HasWidgets() {
return DataObject::get("Widget", "ParentID = $this->WidgetbarID");
}
Then in your template file, MyPage.ss you can use HasWidgets to check whether the WidgetArea should be rendered:
<%if HasWidgets %>
<div id="Widgetbar">
$Widgetbar
</div>
<% end_if %>
You need to have added a WidgetArea to your page (called 'Widgetbar' in this example), otherwise the above will fail and you'll get a white page of death. This code will add a widgetArea to MyPage:
class MyPage extends Page {
static $db = array(
);
static $has_one = array(
"Widgetbar" => "WidgetArea"
);
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Widgets', new WidgetAreaEditor('Widgetbar'));
return $fields;
}
}
Comments
RSS feed for comments on this page RSS feed for all comments
No one has commented on this page yet.
Post a comment ...
You cannot post comments until you have logged in. Login Here.