In SilverStripe any object of type ViewableData
can be rendered. Page is a subclass of ViewableData
, but so is Widget
. A lot of objects in SilverStripe extend ViewableData
.
This means you are already on the right track. I am assuming you have 2 variables:
$request
a request object of typeSS_HTTPRequest
(if you are in a Controller, you can get it like this:$request = $this->getRequest()
)$page
a page object of typePage
or subclass.
(if you are in a Controller, you can get it like this:$page = $this->data()
)
now you should be able to do:
if ($request->isAjax()) {
$widgetArea = $page->SideBar();
$widget = $widgetArea->Widgets()->filter('ClassName', 'MyWidget')->First();
return $widget->renderWith('MyWidgetTemplate');
}
// do something else
NOTE: if $request->isAjax()
is never true, add ?ajax=1
to the URL when you call it in javascript. ?ajax=1
will let SilverStripe know that this is an ajax request.