Template loader taken from this question。尝试在视图文件中使用$this->load->view()
时,除非将返回值指定为true,否则会引发MY_LOADER
的问题。
扩展模板加载器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader {
public function __construct() {
parent::__construct();
}
public function template($template_name, $vars = array(), $return = FALSE) {
if($return) {
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
return $content;
} else {
$this->view('templates/header', $vars);
$this->view($template_name, $vars);
$this->view('templates/footer', $vars);
}
}
}
?>
基本控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Home extends CI_Controller {
public function index() {
$this->load->template('welcome_message');
}
}
welcome_message
<?php echo $this->load->view('account/login'); ?> // Throws error
<?php echo $this->load->view('account/login', array(), true); ?> // No error
如果我没有将返回值定义为 true ,则会抛出错误Object of class MY_Loader could not be converted to string
完整的堆栈跟踪
A PHP Error was encountered
Severity: 4096
Message: Object of class MY_Loader could not be converted to string
Filename: views/welcome_message.php
Line Number: 71
Code on line 71: <?php echo $this->load->view('account/login'); ?>
Backtrace:
File: application/views/welcome_message.php
Line: 71
Function: _error_handler
File: application/core/MY_Loader.php
Line: 17
Function: view
Code on line 17: $this->view($template_name, $vars);
File: application/controllers/Home.php
Line: 26
Function: template
Code on line 26: $this->load->template('welcome_message');
File: index.php
Line: 316
Function: require_once
这种用法在CI.2中正常工作,问题的原因是什么?
答案 0 :(得分:1)
尝试使用下面的视图,而不使用echo
<?php $this->load->view('account/login'); ?>
而不是
<?php echo $this->load->view('account/login'); ?>
Codeigniter视图Fault Tolerance for an Aurora DB Cluster