简体   繁体   English

CodeIgniter将视图加载到数组中

[英]CodeIgniter load view into array

I'm trying to load a view which contains html elements like div etc into an array, but I'm having trouble doing it. 我正在尝试将包含div等html元素的视图加载到数组中,但是这样做很麻烦。 Somehow, even if I replace quotation marks, it seems to be causing error. 以某种方式,即使我替换了引号,也似乎导致了错误。 After loading the view into the array 将视图加载到数组中之后

$tmp =  $this->load->view('details/action_task');
$result['html'] = str_replace('\\"','\\\\"', $tmp);

$resp = json_encode($result);

on chrome console, output is 在chrome控制台上,输出为

{"code":1,"html":""}            
            <div class="text-center">Response</div>

As you can see html is empty, and its "should be" value is out of the json object. 如您所见, html为空,并且其“应为”值不在json对象中。 Any idea on solving this? 有解决的办法吗?

As codeigniter doc provided, 正如codeigniter文档所提供的,

There is a third optional parameter lets you change the behavior of the function so that it returns data as a string rather than sending it to your browser. 第三个可选参数使您可以更改函数的行为,以便它以字符串形式返回数据,而不是将其发送到浏览器。 This can be useful if you want to process the data in some way. 如果您想以某种方式处理数据,这可能会很有用。 If you set the parameter to true (boolean) it will return data. 如果将参数设置为true(布尔值),它将返回数据。 The default behavior is false, which sends it to your browser. 默认行为是false,它将其发送到您的浏览器。 Remember to assign it to a variable if you want the data returned: 如果要返回数据,请记住将其分配给变量:

Try this .. 尝试这个 ..

$tmp =  $this->load->view('details/action_task', '', true);

Hope it will be useful for you. 希望对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM