ThinkPHP模版引擎之变量输出详解
这篇文章主要介绍了ThinkPHP模版引擎的变量输出用法,分析了变量输出的常见用法与使用技巧,非常具有实用价值,需要的朋友可以参考下
本文实例分析了ThinkPHP模版引擎中变量输出的用法。分享给大家供大家参考。具体分析如下:
我们已经知道了在Action中使用assign方法可以给模板变量赋值,赋值后怎么在模板文件中输出变量的值呢?
如果我们在Action中赋值了一个name模板变量:
复制代码 代码如下:
$name = 'ThinkPHP';
$this->assign('name',$name);
使用内置的模板引擎输出变量,只需要在模版文件使用:
{$name}
模板编译后的结果就是
复制代码 代码如下:
最后运行的时候就可以在标签位置显示ThinkPHP的输出结果,注意模板标签的{和$之间不能有任何的空格,否则标签无效。普通标签默认开始标记是 {,结束标记是 },也可以通过设置TMPL_L_DELIM和TMPL_R_DELIM进行更改,例如,我们在项目配置文件中定义:
复制代码 代码如下:
'TMPL_L_DELIM'=>' 'TMPL_R_DELIM'=>'}>',
那么,上面的变量输出标签就应该改成:
后面的内容我们都以默认的标签定义来说明,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程序设计有所帮助。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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.

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.

Python is widely used in a wide range of fields with its simple and easy-to-read syntax. It is crucial to master the basic structure of Python syntax, both to improve programming efficiency and to gain a deep understanding of how the code works. To this end, this article provides a comprehensive mind map detailing various aspects of Python syntax. Variables and Data Types Variables are containers used to store data in Python. The mind map shows common Python data types, including integers, floating point numbers, strings, Boolean values, and lists. Each data type has its own characteristics and operation methods. Operators Operators are used to perform various operations on data types. The mind map covers the different operator types in Python, such as arithmetic operators, ratio

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.

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:
