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
#2

It is the correct way. view() returns string value.

If you want to remove the debug code in the string:
PHP Code:
view('form_view'$data, ['debug' => false]); 
Reply
#3

You get the view in the same way that you did it with CI 3.

PHP Code:
$viewStr view('your_view'$data); 


Let us know how you make out with this.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB