Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
360 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...