search
Homephp教程php手册smarty模板中使用get、post、request、cookies、session变量的方法

{$smarty}保留变量不需要从PHP脚本中分配,是可以在模板中直接访问的数组类型变量,通常被用于访问一些特殊的模板变量。例如,直接在模板中访问页面请求变量、获取访问模板时的时间戳、直接访问PHP中的常量、从配置文件中读取变量等。

1.在模板中访问页面请求变量

我 们可以在PHP脚本中,通过超级全局数组$_GET、$_POST、$_REQUEST获取在客户端以不同方法提交给服务器的数据,也可以通 过$_COOKIE或$_SESSION在多个脚本之间跟踪变量,或是通过$_ENV和$_SERVER获取系统环境变量。如果在模板中需要这些数组,可 以调用Smarty对象中的assign()方法分配给模板。但在Smarty模板中,直接就可以通过{$smarty}保留变量访问这些页面请求变量。 在模板中使用的示例如下所示:

PHP代码

1. {$smarty.get.page} {* PHP方式:$_GET["page"] *}  

2. {$smarty.post.page} {* PHP方式:$_POST["page"] *}  

3. {$smarty.cookies.username} {* PHP方式:$_COOKIE["username"] *}  

4. {$smarty.session.id} {* PHP方式:$_SESSION["id"] *}  

5. {$smarty.server.SERVER_NAME} {* PHP方式:$_SERVER["SERVER_NAME"] *}  

6. {$smarty.env.PATH} {* PHP方式:$_ENV["PATH"]*}  

7. {$smarty.request.username} {* PHP方式:$_REQUEST["username"] *} 
2.在模板中访问PHP中的变量

在PHP脚本中有系统常量和自定义常量两种,同样这两种常量在Smarty模板中也可以被访问,而且不需要从PHP中分配,只要通过{$smarty}保留变量就可以直接输出常量的值。在模板中输出常量的示例如下所示:

1、 {$smarty.const._MY_CONST_VAL} {* 输出PHP脚本中自定义的常量 *}  

2、 {$smarty.const.__FILE__} {* 通过保留变量数组直接输出系统常量 *} 

3、其它

1.{$smarty.now}变量用于访问当前时间戳

可以用 date_format调节器格式化输出. 例如{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}

2.{$smarty.const}

你可以直接访问PHP常量. 例如{$smarty.const._MY_CONST_VAL}

3.{$smarty.capture}

可以通过 {capture}..{/capture}结构 截取的输出可以使用{$smarty} 变量访问.   

4.{$smarty.config}

{$smarty}变量 可以访问已经加载的config变量

例如 {$smarty.config.foo}就可以表示 {#foo#}

5.{$smarty.section}, {$smarty.foreach}

{$smarty} 变量可以访问'section'和'foreach'循环的属性

6.{$smarty.template} 显示当前被处理的模板的名字

7.{$smarty.version}

显示smarty模板的版本

8.{$smarty.ldelim} 显示左分隔符

9.{$smarty.rdelim} 显示右分隔符

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.