Question

I'm working on a system (ActiveCollab) that uses Smarty for templates. I want to output some JSON so I need to have it output by itself... no other cruft. However, if I do NOT provide a Smarty template file for the corresponding action I get an error output that ruins the JSON.

Does anyone know how to disable Smarty output on a case by case basis? In pseudocode, something like

$this->smarty->disable();

Failing that, does anyone know how to have ActiveCollab not require a template file for output? Or even know of a better way to output JSON for consumption within ActiveCollab?

My main goal is to output json for use by jqgrid within ActiveCollab, in case that helps.

Thanks.


EDIT:

It turns out it's as simple as adding die(); after the desired output.

function bar(){
    echo $myoutput;
    die();
}

That's it. Why I always look for the "hard" answer when the solution is so basic I will never know... sigh.

Was it helpful?

Solution

activeCollab will automatically render page layout around page content only if action itself did not provide an exit before it ended execution. This exit may be:

  1. Serving some data with $this->serveData()
  2. Page redirection
  3. Simple printing of content + die()
  4. File forward, using download_file() and download_content() function
  5. Any other exist that stops execution of action (dies within it)

In your case, you can use controller's serveData() method to serve JSON:

function action() {
  $my_json_data = array('something');

  $this->serveData($my_json_data, null, FORMAT_JSON);
}

This function will set proper HTTP headers and encode the value that you are sending.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top