ThinkPHP模版发动机之变量输出详解
ThinkPHP模版引擎之变量输出详解
ThinkPHP模版引擎之变量输出详解
我们已经知道了在Action中使用assign方法可以给模板变量赋值,赋值后怎么在模板文件中输出变量的值呢?
如果我们在Action中赋值了一个name模板变量:
$name = 'ThinkPHP';$this->assign('name',$name);
使用内置的模板引擎输出变量,只需要在模版文件使用:
{$name}
模板编译后的结果就是
<?php echo($name);?>
最后运行的时候就可以在标签位置显示ThinkPHP的输出结果,注意模板标签的{和$之间不能有任何的空格,否则标签无效。普通标签默认开始标记是 {,结束标记是 },也可以通过设置TMPL_L_DELIM和TMPL_R_DELIM进行更改,例如,我们在项目配置文件中定义:
'TMPL_L_DELIM'=>'<{', 'TMPL_R_DELIM'=>'}>',
那么,上面的变量输出标签就应该改成:
<{$name}>
后面的内容我们都以默认的标签定义来说明,assign方法里面的第一个参数才是模板文件中使用的变量名称,如果改成下面的代码:
$name = 'ThinkPHP';$this->assign('name2',$name);
再使用{$name} 输出就无效了,必须使用 {$name2}才能输出模板变量的值了.如果我们需要把一个用户数据对象赋值给模板变量:
$User = M('name');$user = $User->find(1);$this->assign('user',$user);
也就是说$user其实是一个数组变量,我们可以使用下面的方式来输出相关的值:
{$user['name']}//输出用户的名称{$user['email']} //输出用户的email地址
如果$user是一个对象而不是数组的话.
$User = M('name');$User->find(1);$this->assign('user',$User);
可以使用下面的方式输出相关的属性值:
{$user:name}// 输出用户的名称{$user:email} // 输出用户的email地址
3.1版本以后,类的属性输出方式有所调整,支持原生的PHP对象写法,所以上面的标签需要改成:
{$user->name}// 输出用户的名称{$user->email} // 输出用户的email地址
为了方便模板定义,还可以支持点语法,例如,上面的
{$user['name']}// 输出用户的名称{$user['email']} // 输出用户的email地址
可以改成
{$user.name}{$user.email}
因为点语法默认的输出是数组方式,所以上面两种方式是在没有配置的情况下是等效的,我们可以通过配置TMPL_VAR_IDENTIFY参数来决定点语法的输出效果,以下面的输出为例:{$user.name}
如果TMPL_VAR_IDENTIFY设置为array,那么
{$user.name}和{$user['name']}等效,也就是输出数组变量.
如果TMPL_VAR_IDENTIFY设置为obj,那么
{$user.name}和{$user:name}等效,也就是输出对象的属性。
如果TMPL_VAR_IDENTIFY留空的话,系统会自动判断要输出的变量是数组还是对象,这种方式会一定程度上影响效率,而且只支持二维数组和两级对象属性。
如果是多维数组或者多层对象属性的输出,可以使用下面的定义方式:
{$user.sub.name}//使用点语法输出
或者使用
{$user['sub']['name']}// 输出三维数组的值 {$user:sub:name}// 输出对象的多级属性
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
参考来源:
ThinkPHP模版引擎之变量输出详解
http://www.lai18.com/content/369153.html
延伸阅读
《PHP框架ThinkPHP学习》系列技术文章整理收藏
1Thinkphp实现MySQL读写分离操作示例
2使用ThinkPHP+Uploadify实现图片上传功能
3ThinkPHP调用百度翻译类实现在线翻译
4Thinkphp使用mongodb数据库实现多条件查询方法
5ThinkPHP实现多数据库连接的解决方法
6改写ThinkPHP的U方法使其路由下分页正常
7ThinkPHP实现将SESSION存入MYSQL的方法
8ThinkPHP连接数据库及主从数据库的设置教程
9ThinkPHP中pathinfo的访问模式、路径访问模式及URL重写总结
10ThinkPHP基于PHPExcel导入Excel文件的方法
11thinkphp获取栏目和文章当前位置的方法
12ThinkPHP结合AjaxFileUploader实现无刷新文件上传的方法
13ThinkPHP无限级分类原理实现留言与回复功能实例
14ThinkPHP在新浪SAE平台的部署实例
15ThinkPHP控制器间实现相互调用的方法
16ThinkPHP实现带验证码的文件上传功能实例
17ThinkPHP写数组插入与获取最新插入数据ID实例
18ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整
19thinkphp使用literal防止模板标签被解析的方法
20ThinkPHP中RBAC类的四种用法分析
21ThinkPHP中__initialize()和类的构造函数__construct()用法分析
22ThinkPHP自动完成中使用函数与回调方法实例
23ThinkPHP实现动态包含文件的方法
24thinkphp实现发送邮件密码找回功能实例
25ThinkPHP实现支付宝接口功能实例
26Thinkphp搜索时首页分页和搜索页保持条件分页的方法
27ThinkPHP入口文件设置及相关注意事项分析
28ThinkPHP模版引擎之变量输出详解
29thinkphp实现上一篇与下一篇的方法
30ThinkPHP中url隐藏入口文件后接收alipay传值的方法
31ThinkPHP打开验证码页面显示乱码的解决方法
32ThinkPHP中使用ajax接收json数据的方法
33ThinkPHP内置jsonRPC的缺陷分析
34thinkphp3.0输出重复两次的解决方法
35thinkPHP实现表单自动验证
36Thinkphp中的curd应用实用要点
37浅谈thinkphp的实例化模型
38ThinkPHP 404页面的设置方法
39THINKPHP内容分页代码分享
40在Nginx上部署ThinkPHP项目教程
41浅析THINKPHP的addAll支持的最大数据量
42ThinkPHP提示错误Fatal error: Allowed memory size的解决方法
43ThinkPHP3.2.2的插件控制器功能
44ThinkPHP3.2.3数据库设置新特性
45ThinkPHP 3.2 版本升级了哪些内容
46thinkPHP学习笔记之安装配置篇
47Thinkphp调用Image类生成缩略图的方法
48解决ThinkPHP关闭调试模式时报错的问题汇总
49ThinkPHP文件缓存类代码分享
版权声明:本文为博主原创文章,未经博主允许不得转载。
- 1楼u0112524026小时前
- 总结学习

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

"Development Suggestions: How to Use the ThinkPHP Framework to Implement Asynchronous Tasks" With the rapid development of Internet technology, Web applications have increasingly higher requirements for handling a large number of concurrent requests and complex business logic. In order to improve system performance and user experience, developers often consider using asynchronous tasks to perform some time-consuming operations, such as sending emails, processing file uploads, generating reports, etc. In the field of PHP, the ThinkPHP framework, as a popular development framework, provides some convenient ways to implement asynchronous tasks.

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

ThinkPHP is a high-performance PHP framework with advantages such as caching mechanism, code optimization, parallel processing and database optimization. Official performance tests show that it can handle more than 10,000 requests per second and is widely used in large-scale websites and enterprise systems such as JD.com and Ctrip in actual applications.

Development suggestions: How to use the ThinkPHP framework for API development. With the continuous development of the Internet, the importance of API (Application Programming Interface) has become increasingly prominent. API is a bridge for communication between different applications. It can realize data sharing, function calling and other operations, and provides developers with a relatively simple and fast development method. As an excellent PHP development framework, the ThinkPHP framework is efficient, scalable and easy to use.
