The way emanuele is doing it works but in the end it is going to be a lot of duplicitous code. I would also say that you should make it possible for more formats in the future and thus make a start to a real view...
// I am writing this in the reply window so it might be broken.
function viewExit(array $context, $status_code = 200, $template = false)
{
call_integration_hook('integrate_pre_view_exit', array(&$context, &$status_code, &$template));
switch ($GLOBALS['context']['output_strategy'])
{
case 'json':
$output = empty($template) ? json_encode($context) : loadJSONTemplate($template, $context);
break;
case 'xml':
// This obviously requires another function to create a XML map
$output = empty($template) ? xml_encode($context) : loadXMLTemplate($template, $context));
break;
}
call_integration_hook('integrate_view_exit', array(&$context, &$status_code, &$template, &$output);
http_status_code((int) $status_code);
die($output);
}
As soon as I wrote it I wanted to rewrite it but I don't feel like doing it in the post window. The point of the $template is to allow you to change the setup of the $context. The same thing would be done with HTML.
I used a switch, but I would prefer to have a DIC with functions for each strategy. More strategies could be serialized (PHP, igBinary, etc), YAML, Protobuf, INI, custom for debugging, or anything. You don't want to put how your output is handled in every controller.
EDIT: added a gist - https://gist.github.com/joshuaadickerson/5566246