使用CodeIgniter加载视图外视图文件夹 [英] Loading view outside view folder with CodeIgniter

查看:155
本文介绍了使用CodeIgniter加载视图外视图文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从以下范围之外加载视图:

  $ this-> load->视图(); 

看起来工作 base / application / views 目录。如何从 / application / 目录外部访问视图?



我假设我将扩展 CI_Loader类这将是最好的方法吗?



我还找到了保存view_paths的数组:

  // base / system / core / Loader.php 
// CI_Loader
/ **
*从
*
* @var数组加载视图的路径列表
* @access protected
* /
protected $ _ci_view_paths = array();

但是所有声明的变量之上的注释让我陷入了

  //所有这些都是自动设置的。不要混乱他们。 

任何关于从这里出发的想法都非常感谢:-)

解决方案

不知道是否是正确的方法,但它的工作原理:)



application / core 文件夹将此加载程序扩展



 <?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
));
}

}

?>然后你想要一个外部视图文件,假设它在third_party文件夹


$ b < b

application / third_party / my_new_view.php

  ?php echo $ my_name; > 



然后在控制器中调用您的新视图



ext_view 是您的新视图加载程序方法




  • 第一个参数:
  • >
  • 第二个参数:视图名称

  • 第三个参数:变量数据等...



test_controller.php



  $ view_data = array('my_name'=>'dino '); 
$ this-> load-> ext_view('third_party','my_new_view',$ view_data);



如果一切正常。它会输出



Hello:dino


I have the need to load a view from outside the scope of:

$this->load->view();

which appears to work from base/application/views directory. How can I access a view from outside the /application/ directory ?

I assume i will have to extend the CI_Loader class would this be the best way forward ?

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 :)

In your application/core folder put this loader extention

<?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
            ));
  }

}

?>

Then you want a external view file, suppose its in the third_party folder

application/third_party/my_new_view.php

Hello : <?php echo $my_name; ?>

Then call your new view in the controller

ext_view is your new view loader method,

  • 1st param : the folder inside you applicaton
  • 2nd param : the view name
  • 3rd param : the variables data and so on...

test_controller.php

$view_data = array('my_name' => 'dino');
$this->load->ext_view('third_party', 'my_new_view', $view_data);

If everything fine. it will output

Hello : dino

这篇关于使用CodeIgniter加载视图外视图文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆