CI框架与widget(页面布局) 出一份完整的代码
时间: 2024-05-11 20:18:24 浏览: 138
以下是一个使用CI框架和widget(页面布局)的完整代码示例:
1. 控制器文件(application/controllers/Welcome.php)
```php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$data['title'] = 'Welcome to My Website';
$data['content'] = 'This is the homepage of my website.';
$this->load->library('parser');
$$this->parser->parse('welcome_message', $data);
}
}
```
2. 视图文件(application/views/welcome_message.php)
```html
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>{title}</h1>
<p>{content}</p>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
```
3. 页面布局文件(application/widgets/layout.php)
```html
<!DOCTYPE html>
<html>
<head>
<title>{title}</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1>{title}</h1>
{content}
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
```
4. 控制器文件(application/controllers/Welcome.php)中的修改
```php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
public function index()
{
$data['title'] = 'Welcome to My Website';
$data['content'] = 'This is the homepage of my website.';
$this->load->library('parser');
$this->parser->parse('layout', $data);
}
}
```
可以看出,使用widget的方法是将页面内容放入一个占位符中,然后在布局文件中调用这个占位符。这样,在不同的页面中只需要改变占位符的内容就可以了。
阅读全文