我制作简单的模板使用代码codeigniter。 我的代码就像这样.......
这是 template.php 类
class Template {
protected $_ci;
function _construct() {
$this->_ci=&get_instance();
}
function display($template, $data = null) {
$data['_content'] = $this->_ci->load->view($template, $data, TRUE);
$data['_header'] = $this->_ci->load->view('template/header', $data, TRUE);
$data['_top_menu'] = $this->_ci->load->view('template/menu', $data, TRUE);
$data['_right_menu'] = $this->_ci->load->view('template/sidebar', $data, TRUE);
$this->_ci->load->view('/template.php', $data);
}
}
这是我的控制器 welcome.php 类
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct() {
parent::__construct(); //you always have to call parent's constructor before ANYTHING
$this->load->library('template');
$this->load->helper('url');
}
public function index() {
$this->template->display('welcome_message');
}
function contoh_parameter() {
$this->template->display('view_parameter', array('judul' => 'Judul View'));
}
}
这是我的视图类 welcome_message.php class
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
<p class="footer">Page rendered in <strong>{elapsed_time}</strong> seconds</p>
但我得到这样的错误:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/template.php
Line Number: 25
抱歉,我仍然是codeigniter和php的新手。请帮帮我。
答案 0 :(得分:0)
首先,你的构造方法是正确的,它应该是__construct
而不是_construct
其次,在加载视图时使用$this->_ci->load->view('/template.php', $data);
,您不应该在开始路径上使用任何斜杠,以查看CI可用的目录。因此,如果您的视图文件在CI视图文件夹中为 template.php
,则以这种方式$this->_ci->load->view('template', $data);
使用它。您还可以使用.php
扩展名CI self添加该扩展程序。