codeigniter视图

怎么加载视图?

  • 例如我们有一个视图在 application/views/welcome.php
public function index() { $this->load->view('welcome'); } 
  • 视图可以在子目录中, 例如: application/views/html/welcome.php
public function index() { $this->load->view('html/welcome); }

怎么向视图传递动态变量

我们目前有welcome.php这个视图, 现在我们给它的代码是

<html>
<head>
    <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $string; ?></h1> </body>

在渲染视图时怎么传递变量

public function index() { $data['title'] = '你好, 欢迎'; $data['string'] = '我是剑齿虎, 很高兴认识你!'; //view的参数2可以向视图传递数组/对象 $this->load->view('welcome', $data); }

返回视图数据

public function index() { $data['title'] = '你好, 欢迎'; $data['string'] = '我是剑齿虎, 很高兴认识你!'; //view的参数3为一个bool类型, 默认为false. 提供true将返回渲染后的视图数据, 你需要用一个变量来接收它! $template = $this->load->view('welcome', $data, true); var_dump($template); }

视图的继承 (模版引擎? 没有这样的东西)

在网站项目中, 模版的重用比较多(废话). 比如一个网页一般是由”头 主体 侧栏 菜单 尾部”构成的. 因此为避免重复造轮子. 模版继承/视图引入拼接的存在是非常有意义的.

仔细看了看, 好像ci没有模版继承这样的东西. 唯一一个解决方案就是”视图拼接”. 引入多个视图合并

$this->load->view('header', $data); $this->load->view('menu'); $this->load->view('content', $content); $this->load->view('sidebar'); $this->load->view('footer'); //.....

如何在视图中加载其他视图(include)?

需求: 你的 welcome.php 视图, 需要引入一个 side.php 的视图

<html>
<head>
    <title>视图加载视图</title> </head> <body> <?php $this->load->view('side'); ?> </body> </html>

猜你喜欢

转载自www.cnblogs.com/andydao/p/9343221.html