codignieter嵌套视图
问题描述:
我想动态地初始化我的控制器方法并收集我的嵌套视图的输出数据。我想避免每次查看$ data ['something']的实例在这个视图和函数中调用适当的视图php文件。codignieter嵌套视图
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调用正确的方法。什么是添加嵌套视图的最佳方式动态
答
如果您正在使用JQuery,你可以随时使用JQuery $ .load方法这样
$('.sidebar').load('<?php site_url('controllername/methodname')?>');
这里是方法名
function methodname(){
$this->view();
// do any stuff you want.
}
感谢对于你的反馈,主要是我认为我的逻辑是我建立的不是最好的 – fefe 2013-02-26 13:09:03
@fefe我建议你阅读Jamie Rumbelow的My_Controller。它确实是你想要的。他的第一本书是可用的,你可以阅读和学习许多伟大的技术。 – 2013-02-26 13:11:40