从辅助程序Codeigniter加载视图?

问题描述:

我可以从codeigniter中的助手加载视图吗?我一直在寻找一个礼物,但似乎没有人讨论过。

Can i load view from helper in codeigniter? I have been looking for a present, but it seems no one has discussed it.

是的,你可以。创建助手,说 views_helper.php

Yes, you can. Create your helper, say views_helper.php:

if(!function_exists('view_loader')){

  function view_loader($view, $vars=array(), $output = false){
    $CI = &get_instance();
    return $CI->load->view($view, $vars, $output);
  }
}

$ view 是视图文件名(通常使用的), $ vars 你想传递的变量数组a true 作为可选的第三个参数,使其返回(因为它通常会发生)作为内容,而不是只是加载;

$view is the view file name (as you would normally use), and $vars an array of variables you want to pass (as you would normally do), pass a true as optional third parameter to have it returned (as it would normally happen) as content instead of it just being loaded;

只需加载你的帮助器(或自动加载它):

Just load your helper (or autoload it):

$this->load->helper('views');
$data = array('test' => 'test');
view_loader('myview', $data)