本文实例讲述了PHP模板引擎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" 作为参数。
6、保留变量 $smarty.foreach
$smarty.foreach 获取 foreach 循环数组的一些信息,比如数组的个数、递增+1……,关于 $smarty.foreach 的使用,详细内容可参考前面一篇《PHP模板引擎Smarty内建函数foreach,foreachelse用法分析》
更多关于PHP相关内容感兴趣的读者可查看本站专题:《smarty模板入门基础教程》、《PHP模板技术总结》、《PHP基于pdo操作数据库技巧总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于smarty模板的PHP程序设计有所帮助。

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor

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),

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.