本文实例分析了ThinkPHP模版引擎中变量输出的用法。分享给大家供大家参考。具体分析如下:
我们已经知道了在Action中使用assign方法可以给模板变量赋值,赋值后怎么在模板文件中输出变量的值呢?
如果我们在Action中赋值了一个name模板变量:
$this->assign('name',$name);
使用内置的模板引擎输出变量,只需要在模版文件使用:
{$name}
模板编译后的结果就是
最后运行的时候就可以在标签位置显示ThinkPHP的输出结果,注意模板标签的{和$之间不能有任何的空格,否则标签无效。普通标签默认开始标记是 {,结束标记是 },也可以通过设置TMPL_L_DELIM和TMPL_R_DELIM进行更改,例如,我们在项目配置文件中定义:
那么,上面的变量输出标签就应该改成:
后面的内容我们都以默认的标签定义来说明,assign方法里面的第一个参数才是模板文件中使用的变量名称,如果改成下面的代码:
$this->assign('name2',$name);
再使用{$name} 输出就无效了,必须使用 {$name2}才能输出模板变量的值了.如果我们需要把一个用户数据对象赋值给模板变量:
$user = $User->find(1);
$this->assign('user',$user);
也就是说$user其实是一个数组变量,我们可以使用下面的方式来输出相关的值:
{$user['email']} //输出用户的email地址
如果$user是一个对象而不是数组的话.
$User->find(1);
$this->assign('user',$User);
可以使用下面的方式输出相关的属性值:
{$user:email} // 输出用户的email地址
3.1版本以后,类的属性输出方式有所调整,支持原生的PHP对象写法,所以上面的标签需要改成:
{$user->email} // 输出用户的email地址
为了方便模板定义,还可以支持点语法,例如,上面的
{$user['email']} // 输出用户的email地址
可以改成
{$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}// 输出对象的多级属性
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
