ThinkPHP 3.1.2 视图 2

一、模板的使用        (重点)
	a、规则
		模板文件夹下[TPL]/[分组文件夹/][模板主题文件夹/]和模块名同名的文件夹[Index]/和方法名同名的文件[index].html(.tpl)
		更换模板文件的后缀名(修改配置文件)
		'TMPL_TEMPLATE_SUFFIX'=>'.html',//更改模板文件后缀名
	b、修改模板文件目录层次
		'TMPL_FILE_DEPR'=>'_',//修改模板文件目录层次
	c、模板主题
		'DEFAULT_THEME'=>'your',//设置默认模板主题
		需要在TPL下面新建一个your文件夹作为模板主题文件夹
		如何动态修改模板主题?
		1、在后台准备一个功能,修改config.php文件中的默认模板项
		2、通过url传递 t=主题 参数可以修改不同的模板
			'DEFAULT_THEME'=>'your',//设置默认模板主题
			'TMPL_DETECT_THEME'=>true,//自动侦测模板主题
			'THEME_LIST'=>'your,my',//支持的模板主题列表
		
二、输出模板内容      (重点)
	a、display
		1.display中没有参数
			$this->display();
		2.可以带参数
			$this->display(本模块文件夹下的其他模板文件);
			$this->display('index2');

			$this->display(其他文件夹下的模板文件);
			$this->display('Public:error');//注意,仅仅需要在Tpl下有Public文件夹以及其中的error.html即可,不需要一定有Public模块

			$this->display(其他主题下的 文件夹下的 模板文件);//需要开启主题支持
			$this->display('my:Index:index');

			$this->display(一个url路径);
			$this->display('./Public/error.html');

			$this->display('./Public/error.html','utf-8','text/xml');

			$this->show($content);
		3.fetch方法
			获得模板文件中的内容,以字符串形式返回
			$content=$this->fetch('Public:error');
		4.show方法
			不需要模板文件,可以直接输出模板内容
				$content=$this->fetch('Public:error');
				dump($content);
				$content=str_replace('h1','i',$content);
				$this->show($content);
三、模板中的赋值      (重点)
		//$this->assign('name','赵桐正');
		$this->name='赵桐正2';
		$this->display();
四、模板替换          (重点)
__PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/
__ROOT__: 会替换成当前网站的地址(不含域名) 
__APP__: 会替换成当前项目的URL地址 (不含域名)
__GROUP__:会替换成当前分组的URL地址 (不含域名)
__URL__: 会替换成当前模块的URL地址(不含域名)
__ACTION__:会替换成当前操作的URL地址 (不含域名)
__SELF__: 会替换成当前的页面URL
		
		更换模板变量规则,修改配置项
		'TMPL_PARSE_STRING'=>array(           //添加自己的模板变量规则
		'__CSS__'=>__ROOT__.'/Public/Css',
		'__JS__'=>__ROOT__.'/Public/Js',
	),



//



C:wampwww	hinkphpPublicerror.html

//当前目录下的Public下的error.html,当前目录是指C:wampwww	hinkphp

$this->display('./Public/error.html');


访问:
http://localhost/thinkphp/index.php/Index/show


$this->display 需要一个模板文件,不然报错


$this->show($content) 不需要模板

//

    public function index(){
		//$content=$this->fetch('Public:error');
		
		$this->assign('name','赵杨健');
		//$this->show($content);
		 $this->display();
	
	
	}


//前台模板:

 <h1>helloworld---22--33-44 <{$name}></h1>


// 引入css文件
//目录结构:

$ ls -ltr
总用量 2
-rwxrwx---+ 1 Administrators None 321 四月 14 13:50 error.html.bak
-rwxrwx---+ 1 Administrators None 332 四月 14 15:13 error.html
drwxrwx---+ 1 Administrators None   0 四月 14 16:54 Js
drwxrwx---+ 1 Administrators None   0 四月 14 17:01 Css


$ cd Css/

Administrator@QCEE61NZ66FEX2D /cygdrive/c/wamp/www/thinkphp/Public/Css
$ ls
basic.css


Administrator@QCEE61NZ66FEX2D /cygdrive/c/wamp/www/thinkphp/Public
$ cd Js/

Administrator@QCEE61NZ66FEX2D /cygdrive/c/wamp/www/thinkphp/Public/Js
$ ls
basic.js  basic.js.bak



Index/index.html:

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Index</title>
  <link rel='stylesheet' type='text/css' href='__PUBLIC__/Css/basic.css'/>
 </head>
 
 <body>
 <p>helloworld---22--33-44 <{$name}></p>
 
 
 
<!--  <volist name='data' id='vo'>
			<{$vo.id}>----<{$vo.username}>-----<{$vo.sex}><br/>
		</volist> -->
 </body>
</html>






//css文件内容:

p {
 background:green;
 }


//调用 js文件
alert('我被调用了');










//**********************************


	echo "test";
	$this->display('Public:error');
    }

	
	访问Public目录下的error文件
	
	

	php 访问公告页面:
	
	C:wampwww	hinkphpPublicerror.html
	
	$this->display('./Public/error.html');
	
	./当前目录 是指index.php 主入口文件
		
	指定浏览器的编码格式:

	//$this->display('./Public/error.html','UTF-8');
	$this->display('./Public/error.html','GBK');

	
	/****使用show 方法
	
	display方法必须有一个模板文件才行,如果没有模板文件的话
	
	可以使用show方法
	
	class IndexAction extends Action {
    public function index(){

		echo "test";
	
	
	$this->show('aaabbdd');
	
    }

	
/****************

class IndexAction extends Action {
    public function index(){

		echo "test";
	
	$content=$this->fetch('Public:error');
	$this->show("$content");
	
    }

	
	Public文件下的error文件
	
	

	class IndexAction extends Action {
    public function index(){

		echo "test";
	
	$content=$this->fetch('Public:error');
	$content=str_replace('h1','i',$content);
	$this->show("$content");
	
    }
	
	fetch 获得模板文件中的内容,以字符串的形式返回
	
	4.show 方法
	
	不需要模板文件,可以直接输出模板内容
	
	class IndexAction extends Action {
    public function index(){

		// echo "test";
	
	// $content=$this->fetch('Public:error');
	// $content=str_replace('h1','i',$content);
	// $this->show("$content");
	
	$this->assign('name','赵杨健');
	$this->display();
	
    }
	
	变量分配给前台
	
	<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 </head>
 <body>
 Hello <{$name}>
 

 </body>
</html>
	

/**** 调用样式,样式直接写在页面之中

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
  <link rel='stylesheet' type='text/css' href='__PUBLIC__/Css/basic.css'/>
 </head>
 <body>
 <p>Hello <{$name}></p>
 

 </body>
</html>


/**** 调用样式,引入外部CSS

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 <link rel='stylesheet' type='text/css' href='__PUBLIC__/Css/basic.css'/>
 </head>
 <body>
 <p>Hello <{$name}></p>
 

 </body>
</html>



 <link rel='stylesheet' type='text/css' href='/thinkphp/Public/Css/basic.css'/>
 </head>
 <body>
 <p>Hello 赵杨健</p>

说明__PUBLIC__ //表示Public的文件目录 	 
	
	
	
	
	

 __PUBLIC__:会被替换成当前网站的公共目录 通常是 /Public/ <br/>
__ROOT__: 项目地址
__APP__: 会替换成当前项目的URL地址 (不含域名) <br/>
__GROUP__:会替换成当前分组的URL地址 (不含域名) <br/>
__URL__: 会替换成当前模块的URL地址(不含域名) <br/>
__ACTION__:会替换成当前操作的URL地址 (不含域名) <br/>
__SELF__: 会替换成当前的页面URL


/thinkphp/Public:会被替换成当前网站的公共目录 通常是 /Public/ 
/thinkphp: 会替换成当前网站的地址(不含域名) 
/thinkphp/index.php: 会替换成当前项目的URL地址 (不含域名) 
/thinkphp/index.php:会替换成当前分组的URL地址 (不含域名) 
/thinkphp/index.php/Index: 会替换成当前模块的URL地址(不含域名) 
/thinkphp/index.php/Index/index:会替换成当前操作的URL地址 (不含域名) 
/thinkphp/index.php/Index/index: 会替换成当前的页面URL	
	

/** 引入js


<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>Document</title>
 <link rel='stylesheet' type='text/css' href='__PUBLIC__/Css/basic.css'/>
 
 <script src='__PUBLIC__/Js/basic.js'></script>
 </head>
 <body>
 <p>Hello <{$name}></p>
 

 </body>
</html>