sonic icon indicating copy to clipboard operation
sonic copied to clipboard

Rendering partial views via ajax

Open gordyr opened this issue 13 years ago • 1 comments

I'm attempting to render single view or turbo fragment on demand so that I am not re-rendering the full layout/page when it isn't necessary.

So far I have set up the following:-

Lets image I am currently at www.example.com

I click a link and rather than reload the whole page an ajax request is sent to www.example.com/home

this is routed to /ajax/home (and the full page including layout is routed to /home)

What I would like to do is extract the html (and css/js set in the view) and return it via JSON.

I am trying to do the following:

public function home() {

}

public function ajaxhome()
{

    $this->setView('home');       
    $this->disableLayout();


}

This sends back the html which I can then replace an element with. Great.

However I want to put this html into a json array so that I can also collect the relevant css and javascript:-

public function ajaxhome() {

    $this->setView('home');       
    $this->disableLayout();
    $test = $this->view->getHtml();

    $output = array(
                 'html' => $test
                );

    echo json_encode($output);

}

But it doesn't work. result['html'] is always null. Where am I going wrong?

Even if I get this working I still need to collect the css/JS so what I would really like to do is be able to built a full turbo fragment on demand.

Is there any way this can be achieved?

gordyr avatar Jul 06 '12 09:07 gordyr

Nevermind! I found what I needed from hunting through the sourcecode...

$test = $this->view->outputAsJson();

Perfect!

gordyr avatar Jul 06 '12 10:07 gordyr