1

I've got a custom controller in my plugin that checks whether or not the request is ajax or not, using craft()->request->isAjaxRequest()

In the event that it is, I just want it to return a partial. First though, I'm trying to just get it to return a string but seem to be having issues getting something as simple as that to work (I'm pretty new to PHP). The code I have on the condition of it being an ajax request is:

$path = craft()->path->getSiteTemplatesPath();
craft()->path->setTemplatesPath($path);
$temp = craft()->templates->render('notifications/_test');
echo $temp;

I'm logging the response in javascript but keep getting a 404. If I try to open the link (custom controller url) in a new tab (so not and ajax request) it correctly executes the non ajax condition.

The GET request looks like this: $.get($(this).attr('href'), function(data)... where the href is the controller action url.

Does anything look amiss? Am I perhaps missing a step to allow for this to work? I've tried $allowAnonymous on the controller to no avail.

0

You can't echo out of the controller function. Though, you can send back the rendered template via a convenient controller function.

$this->renderTemplate('notifications/_test');

To add variables for your template to access (because it's not automatic for security reasons). You can do this.

$this->renderTemplate('notifications/_test', array(
    'message' => $message
));
|improve this answer|||||
  • You legend, thank you :) Feeling kind of silly as it was simpler than everything I'd tried.... – user4041 Sep 11 '15 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy