Welcome Guest, Not a member yet? Register   Sign In
How to properly get the view as a string
#1

Hi all, 
My goal is to get a piece of (rendered) HTML code inside a JSON response.  Something like that:
Code:
{
"status": "success",
"html" : "<h1>Today is your birthday</h1>"
}

This is just an example, the real HTML is a little bit longer and has variables inside, so I need to create a view file.

In CI3 there is the third param flag this command to output the HTML as a string:
PHP Code:
$string $this->load->view('you_view'$paramsTRUE); 

Now, I'm trying to understand how things are working in CI4 and choose the right way.

This is my code right now. 
Is this the correct way or is there something that I'm missing (like the View Renderer)?
PHP Code:
$data = [
      'api' => $this->api
      'formId' => $this->formId
];

$html view('form_view'$data);

return 
$this->response->setJSON([
            'status'            => 'success',
            'html'              => $html
        
]); 

Thanks for the advice.
Reply


Messages In This Thread
How to properly get the view as a string - by Jag81 - 12-14-2021, 07:16 AM



Theme © iAndrew 2016 - Forum software by © MyBB