【概述】
- 使用视图模板时,控制器类是继承extend Controller_Template
- 类中需定义 导入的模板视图路径 并赋值给public $template变量
- 控制器函数中使用
$this->template->test="hello world";
意思就是向模板视图中传入一个值为"hello world"
的变量test - 模板视图中通过
$test
来读取test
变量值;
使用非模块视图时,继承的是extends Controller
步骤如下:
1、先设置需要渲染拉取的视图
$view=View::factory("home/index");
2、使用bind或set方法传送数据到该视图 如:
$view=View::factory("home/index")
->set("tt","非模块视图")
->bind("gg","bind传送")
//等价
$view=View::factory("home/index")
$view ->tt="非模块视图"
//视图页面通过$tt $gg来读取数据
3 、通过$this->response->body($view);
来渲染视图
【代码】
控制器Controller
<?php defined('SYSPATH') or die('No direct script access.');
//继承普通控制器
class Controller_Test extends Controller {
public function action_index()
{
//传送数据到非模板view视图层test.php中,该视图下使用数据kk方式为<?php echo $? >
//set等价的三种方法:
//1.
$view=View::factory('test')->set('kk','hello kk');
//2.
$view=View::factory('test')
$view->kk="hello kk";
//3
$view=View::factory('test')
$view->set("kk","hello kk");
//bind方式 第二种是错误的
//1.
$view=View::factory('test');
$gg="ggggggg";
$view->bind('gg',$gg);
//2.
//$view->bind('gg','ggggggggg');//错误第二个参数必须是变量;
$this->response->body($view);
}
} // End Welcome
控制器Controller_Template
<?php defined('SYSPATH') or die('No direct script access.');
//继承模板控制器,一般用于视图View层页面有相同的板块,通俗点来说就是将多个view页面共同的抽离出来作为模板视图,然后和非模板视图组合成最终的页面。
class Controller_Testone extends Controller_Template{
public $template="tpl/mobanview";//导入需要的模板视图
public function action_index()
{ //public $tt;
$content = View::factory('indexview');
//传送数据到非模板视图indexviewp.php中,该视图使用数据fei方式为<?php echo $fei? >
$content->set("fei","非模板数据ttttttttt");
//将非模块视图配置到模块视图的一个contents变量中,
//模块视图可以使用<?php echo $contents ? >拉入非模块视图到模块中
$this->template->contents = $content;
//向模块中传输数据 模块中使用数据<?php echo $yys;echo $hh;? >
$this->template->yes='模板数据';
$this->template->hh="hhhhhhhhh";
}
}