本文实例分析了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程序设计有所帮助。

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl


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

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

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

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
