Sepp Hofer Sepp Hofer - 10 days ago 5
Ajax Question

Silverstripe 3.4 load content via ajax / Shortcode issue

Shortcodes dont render correctly.

I am loading content via ajax into bootstrap-tabs.

calling ajax works.

routes are ok.

I've tried

renderWith()
,
customise()
....

No matter what i try, the shortcodes don't render. Please help.

class EventPage_Controller extends Page_Controller {

public function EventDetail(SS_HTTPRequest $request){

if($request){

$group = DataObject::get_one('Group', "\"Code\" = 'eventgroup'");
$member = Member::currentUser();
if($member && $member->inGroup($group->ID)){

if($event = DataObject::get_by_id('Event', intval($request->param('eventID')))){

$eventDetail = array(
'EventTitle' => $event->Name,
'EventContent' => $event->Content
);

/* if( Director::is_ajax() ) {
//$return = array('Time' => $time);
return $this->renderWith('EventDetail', json_encode(array('EventContent' => $event->Content)));
}*/
//return $this->customise($eventDetail)->renderWith(array('EventDetail', 'Page'));
//echo $event->Content;

echo json_encode(array('error' => '0', 'EventContent' => $event->Content));

}
}
}
}


javascript:

$('#event-detail').hide();
$('.event-detail-btn').on('click', function(e){
//alert('click');
e.stopPropagation();
e.preventDefault();
if($(this).parent().is('tr')){
//alert('TR');
var eventID = $(this).parent().attr('id').split('_')[1];
var dateID = $(this).parent().attr('id').split('_')[2];
//alert(eventID + '_' + dateID);
getEventDetail(eventID, dateID);
}
$('#event-detail, #member-enrolment').toggle();
});


function getEventDetail(eventID, dateID){

//$('#eventDetail').load('Enrolment/EventDetail/'+ eventID + '/' +dateID);

$.ajax({
type: "POST",
url: 'Enrolment/EventDetail/'+ eventID + '/' +dateID,
dataType: "json"
}).success(function(msg){
$('#eventDetail').html(msg.EventContent);
});
}


Page.ss

<div id="enrolments" class="tab-pane fade">


<% include EventDetail %>
<% include MemberEnrolments %>


</div> <!--end enrolments-->


EventDetail.ss

<div id="event-detail">
<br><br>
<a href="#"><button type="button" class="event-detail-btn btn btn-default btn-sm">zurück</button></a>
<br><br>

<div id="eventDetail"></div>

<a href="#"><button type="button" class="event-detail-btn btn btn-default btn-sm">zurück</button></a>
</div>


enter image description here

wmk wmk
Answer

Shortcodes are not automatically rendered when you're not calling them from a template, try:

$eventDetail = array(
    'EventTitle' => $event->Name,
    'EventContent' => $event->Content->forTemplate() //this is a HTMLText
);

or on SS3.4:

$eventDetail = array(
    'EventTitle' => $event->Name,
    'EventContent' => $event->Content->RAW() //this is a HTMLText
);

If this still doesn't work you might do it the hard way and call ShortcodeParser manually:

ShortcodeParser::get_active()->parse($event->Content);

Also, you might use Director::is_ajax() to check if it's really an ajax call and output the ajax response or - as a fallback - the whole page including ajax content. See lessons or docs

Comments