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这个视图, 如今咱们给它的代码是php

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

 

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

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没有模版继承这样的东西. 惟一一个解决方案就是”视图拼接”. 引入多个视图合并app

$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 的视图ide

<html> <head> <title>视图加载视图</title> </head> <body> <?php $this->load->view('side'); ?> </body> </html>
相关文章
相关标签/搜索