Welcome Guest, Not a member yet? Register   Sign In
render views for json encoding
#1

[eluser]coolant[/eluser]
Is it possible to load views into variables and then render them via json?

i.e...

Code:
$data['name'] = 'John';

$json['header'] = $this->view( 'header', $data );
$json['page'] = $this->view( 'page' );
$json['footer'] = $this->view( 'footer', $data );

echo json_encode($json);
#2

[eluser]Seppo[/eluser]
yes, you can fetch the view instead of outputing it, setting a third parameter to TRUE.
You also need to use $this->load->view instead of $this->view, although this last one works on PHP 4, but not on PHP 5.
And instead of "echo" you can use $this->output->set_output(json_encode($json))
#3

[eluser]coolant[/eluser]
Thank you very much! That was a very quick response and extremely helpful Smile
#4

[eluser]coolant[/eluser]
can you do the same with $this->output->enable_profiler(true) ? The ability to assign it to a variable.
#5

[eluser]Seppo[/eluser]
No... the profiler is something else... if you are gonna send json you can't enable it, because it output debug information
#6

[eluser]Pascal Kriete[/eluser]
I think coolant is asking if he can get the profiler output written to a variable. The answer is still no though, because the profiler is created after the controller has finished executing.
#7

[eluser]Seppo[/eluser]
Thanks, inparo... I just didn't get it...
Wouldn't it work doing it manually? I can't test now, but this might work...

Code:
$this->load->library('profiler');
$data['profiler'] = $this->profiler->run();
#8

[eluser]Pascal Kriete[/eluser]
Oh, very clever

That does work, as long as it's the last thing you do (obviously).
#9

[eluser]coolant[/eluser]
Perfect! Smile Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB