JianYA
JianYA

Reputation: 3024

CodeIgniter loading view via url or via file_get_contents

I was wondering if it's possible to load a view via a url or file_get_contents? I have a html file that sits outside my Codeigniter application in the Document Root in a folder that can be accessed in public.

I have tried

$body = $this->load->view('http://dev.app.com/Assets/2018/03/411aa377c0e092ce.html',$data,true);

and

$email = $this->EmailsModel->get_email_by_id($campaign->email_id);
$email_contents = file_get_contents($email['file_location']);
$body = $this->load->view($email_contents,$data,true);

Both give me this error Unable to load the requested file:

Upvotes: 2

Views: 1172

Answers (2)

BigJ
BigJ

Reputation: 31

You could do a curl request to get the html response of that file ( residing outside your application). Make a view file and display the data it received. Now load this view file and pass the response to this view file.

Upvotes: 0

Alex
Alex

Reputation: 9265

You can't load a view via a string wherever it comes from and add data to it as you normally would (as a string can't be compiled as php). You can append strings via $this->output->append_output($str) however this will not allow you to pass any parameters to be replaced and you can't return it as it just sets the output.

You could use the parser library of CI and have your relevant variables in the "view" look like {some_var} and pass data to the view as you would normally do utilizing the parse_string() function of the parser library.

You also might be able to modify the protected $_ci_view_paths array seen in the loader file in /system/core to include the assets directory as a valid path to check for views and then just use 2018/03/411aa377c0e092ce.html to load your view but I haven't tested this and am not sure if it would work.

Similar approach seen here: Loading view outside view folder with CodeIgniter

Upvotes: 2

Related Questions