使用 CodeIgniter 加载视图文件夹外的视图 [英] Loading view outside view folder with CodeIgniter
问题描述
我需要从以下范围之外加载视图:
I have the need to load a view from outside the scope of:
$this->load->view();
似乎在 base/application/views
目录下工作.如何从 /application/
目录之外访问视图?
which appears to work from base/application/views
directory. How can I access a view from outside the /application/
directory ?
我想我将不得不扩展 CI_Loader 类
这是最好的方法吗?
I assume i will have to extend the CI_Loader class
would this be the best way forward ?
我还找到了保存 view_paths 的数组:
I have also found the array which holds the view_paths:
// base/system/core/Loader.php
// CI_Loader
/**
* List of paths to load views from
*
* @var array
* @access protected
*/
protected $_ci_view_paths = array();
但是上面所有声明变量的注释让我卡住了
but the comment above all the declared variables has got me stuck
// All these are set automatically. Don't mess with them.
任何关于从这里去哪里的想法将不胜感激:-)
Any ideas on where to go from here would be greatly appreciated :-)
推荐答案
不知道是不是正确的方法,但是有效 :)
Don't know whether it's a correct way, but it works :)
<?php
class MY_Loader extends CI_Loader {
function ext_view($folder, $view, $vars = array(), $return = FALSE) {
$this->_ci_view_paths = array_merge($this->_ci_view_paths, array(APPPATH . $folder . '/' => TRUE));
return $this->_ci_load(array(
'_ci_view' => $view,
'_ci_vars' => $this->_ci_object_to_array($vars),
'_ci_return' => $return
));
}
}
?>
然后你想要一个外部视图文件,假设它在第三方文件夹中
application/third_party/my_new_view.php
Hello : <?php echo $my_name; ?>
然后在控制器中调用您的新视图
ext_view 是您的新视图加载器方法,
Then call your new view in the controller
ext_view is your new view loader method,
- 第一个参数:应用程序中的文件夹
- 第二个参数:视图名称
- 第三个参数:变量数据等等...
$view_data = array('my_name' => 'dino');
$this->load->ext_view('third_party', 'my_new_view', $view_data);
如果一切正常.它会输出
你好:恐龙
这篇关于使用 CodeIgniter 加载视图文件夹外的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!