1
votes

By default, SocialENgine automatically renders a view at the end of each controller action. If you're using a layout it also renders that. This is fine for normal Web pages, but when you're sending an AJAX response you don't want all that. How do you prevent SocialEngine from auto-rendering on an action-by-action basis? With ZendFramework, you can do this:

$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender(TRUE);

And you have access to some nice helpers like:

$this->_helper->json($data);

... which will json_encode your $data, output it with JSON headers and die at last, so getting clean JSON returned from the action without view rendering which is pretty much just what I want.

However none of these solutions work in SocialEngine -- $this->_helper always seems to be an empty array as does $this->view->_helper. And return $this->setNoRender() only prevents the current element from rendering, not the whole page.

2

2 Answers

2
votes

in some default widget I found this function call, which suppresses the rendering of a widget completely:

return $this->setNoRender();

It's implemented in Engine_Content_Widget_Abstract in case you're interested.

Best, Benni.

0
votes

I encountered this problem in the context of a widget controller.

My solution to get access to the json() helper is to make and instantiate a concrete class extending the abstract Core_Controller_Action_Standard, and expose a wrapper method to send the json in the widget's Controller.php file:

class Widget_MyWidgetAjaxController extends Core_Controller_Action_Standard
{
    public function sendjson($data) {
        return $this->_helper->json($data);
    }
}

This is used in the Widget's controller like this:

class Widget_MyWidgetController extends Engine_Content_Widget_Abstract
{
    public function indexAction() 
    {
        // This is an ajax request to our widget controller
        if ($this->getRequest() && $this->getRequest()->isPost()) {

            // Do whatever processing you need 
            // [...]
            $response_data=array('hello','world');

            $ajax = new Widget_TrackablesAjaxController(
                $this->getRequest(), 
                new Zend_Controller_Response_Http
            );

            return $ajax->sendjson($response_data);
        }
    }
}

I did not have to explicitly disableLayout() or setNoRender(). Responding via _helper->json() is sufficient.