PHP 中使用 Smarty 之三:Smarty中的保留变量
在Smarty 中,有一些保留变量,它们是不需要PHP 脚本去分配就可以直接使用,即不用使用$_tpl->assign('var','value') 去分配。
1、在模板中访问页面请求的变量
{$smarty.get.user} == $_GET['user']
{$smarty.post.user} == $_POST['user']
{$smarty.cookie.username} == $_COOKIE['username']
{$smarty.session.username} == $_SESSION['username']
{$smarty.server.REMOTE_ADDR} == $_SERVER["REMOTE_ADDR"]
{$smarty.env.PATH} == $_ENV['PATH']
{$smarty.request.username} == $_REQUEST['username']
2、在模板文件中访问PHP 脚本中定义的常量和系统常量
{$smarty.const.__FILE__} 当前执行的PHP 文件
{$smarty.const.CONST_VAR} 访问PHP 脚本中define 定义的常量
3、在模板文件中获取当前服务器的时间
{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} == PHP脚本中的date('Y-m-d H:i:s',time())
4、配置文件在模板中的访问
可以通过{$smarty.config.配置变量} 来访问配置文件中的模板变量。这样的话,在模板中访问配置文件中的变量就有两种方法:一、{#配置变量#};二、{$smarty.config.配置变量},如果有区域的话,也是如此。
5、获取Smarty 内建函数capture 捕获的数据
capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面,任何在{capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定。在模板中通过$smarty.capture.foo 访问该变量,如果没有指定name 属性,函数默认将使用"default" 作为参数
摘自:Lee.的专栏

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
