开发者

How to modify data in array before sending to view?

I currently send some data as an array from my CodeIgniter controller to a view:

//Send data to template
$this->load->view('generator/content', $data);

Where $data is:

$data = array_merge($page, $posts);

I wish to modify some of the data in $posts before sending it to the view, by calling a function/method inside my library:

if (!empty($posts)) {   
    $posts = $this->my_library->modifyPosts($posts, $page_ID);
}

The stuff inside the function/method includes str_replace, exploding strings, time formatting and generally turning the data from the array into a usable format. From what I understand, it's best to do this stuff using a library.

Is there a way I can rebuild and return the array to the controller from the library, 开发者_开发问答so that when I pass the data to the view it is ready to be presented?


Well you should be calling the library function from the controller anyway. So in the controller you might have

$posts = $this->my_library->modify_something($posts);

and then your library can do it's thing and return $posts in the new "usable format"

Then it's just fine from there, add the data to the $data array and pass it to your view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜