codignieter嵌套视图

时间:2013-02-26 12:21:02

标签: codeigniter view controller

我正在尝试动态初始化我的控制器方法并收集嵌套视图的输出数据。我想避免每次检查$ data ['something']的实例在视图和函数中调用相应的视图php文件。

class Admin extends CI_Controller {

    function __construct()
    {
        parent::__construct();
    }

    public function _remap($method, $params = array())
    {
        $this->view($method, $params);
    }
    public function view($method, $params)
    {
        $param = (implode(",", $params));
        $data = array();
        if (method_exists($this,$method) ){
            $param = !empty($param) ? $param : '';
            $data = call_user_func_array(array($this, $method), array($param));
            // and load the data array in the main view
            $this->load->view('admin/dashboard', $data);
        } else {
            $this->load->view('admin/404', $data);
        }
    }

    public function _one_of_mymethod(){
        $data['nested_view'] = 'view1';
        //calling models gather $data
        return $data;
    }

}

比数据数组的功能输出正确的嵌套视图。

主视图

会像

<div class="sidebar"><?php $this->view('admin/sidebar'); ?></div>
<div id="content"><?php $this->view('/admin/'. $nested_view) ?></div>

但是,如果我使用ajax调用,这似乎不是正确的方法。动态添加嵌套视图的最佳方法是什么

1 个答案:

答案 0 :(得分:0)

如果你使用的是JQuery,你总是可以像这样使用JQuery $ .load方法

$('.sidebar').load('<?php site_url('controllername/methodname')?>');

这是方法名称

function methodname(){
    $this->view();
   // do any stuff you want.
}
相关问题