assign('name',$name);$this->display();?2.在模板中直接输出:Hello,{$name} !?编译后的结果是:Hello,"/> assign('name',$name);$this->display();?2.在模板中直接输出:Hello,{$name} !?编译后的结果是:Hello,">
Home > Backend Development > PHP Tutorial > ThinkPHP数据模板展示——普通变量

ThinkPHP数据模板展示——普通变量

WBOY
Release: 2016-06-13 12:12:16
Original
983 people have browsed it

ThinkPHP数据模板显示——普通变量

一、变量输出

1.在控制器中给变量赋值:

$name = "ThinkPHP";$this->assign('name',$name);$this->display();
Copy after login

?2.在模板中直接输出:

Hello,{$name} !
Copy after login

?编译后的结果是:

Hello,<?php echo($name);?>
Copy after login

?运行时在模板中显示:Hello,ThinkPHP !

注:

(1)模板标签的 { 和 $ 之间不能有任何空格,否则标签失效。

?

(2)普通标签默认开始标记是 { ,结束标记是 }。也可以通过TMPL_L_DELIM 和 TMPL_R_DELIM进行更改。eg:在项目的配置文件中定义:

'TMP_L_DELIM' => '<{','TMP_R_DELIM' => '}>',
Copy after login

?上面的输出标签就可以改成: Hello,<{$name}> !

?

(3)模板标签的变量输出根据变量类型有所区别,上面输出的是字符串变量,如果是数组变量:

控制器:  $data['name'] = "ThinkPHP";	  $data['emai'] = "[email&#160;protected]";          $this->assign('data',$data);  $this->display();模板: 	  Name  : {$data.name} <br/>             Email : {$data.email}或者      Name  : {$data['name']} <br/>          Email : {$data['email']}
Copy after login

?如果data变量是一个对象(并且包含有name和email两个属性),可以用下面的方式输出:

 Name  : {$data:name}     Email : {$data:email}    或者     Name  : {$data->name}	 Email : {$data->email}
Copy after login

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template