Home > php教程 > php手册 > body text

PHP模板引擎Smarty中的保留变量用法分析,模板smarty

WBOY
Release: 2016-06-13 08:42:01
Original
827 people have browsed it

PHP模板引擎Smarty中的保留变量用法分析,模板smarty

本文实例讲述了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']

Copy after login

2、在模板文件中访问 PHP 脚本中定义的常量和系统常量

{$smarty.const.__FILE__}  //当前执行的 PHP 文件
{$smarty.const.CONST_VAR}  //访问 PHP 脚本中 define 定义的常量

Copy after login

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模板引擎Smarty内建函数详解
  • PHP模板引擎Smarty内置变量调解器用法详解
  • PHP模板引擎Smarty自定义变量调解器用法
  • PHP模板引擎Smarty内建函数foreach,foreachelse用法分析
  • PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例
  • PHP模板引擎Smarty中变量的使用方法示例
  • smarty模板引擎从php中获取数据的方法
  • ThinkPHP使用smarty模板引擎的方法
  • 在PHP模板引擎smarty生成随机数的方法和math函数详解
  • PHP模板引擎Smarty的缓存使用总结
  • php smarty模板引擎的6个小技巧
  • [PHP]模板引擎Smarty深入浅出介绍
  • PHP模板引擎Smarty内建函数section,sectionelse用法详解
Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!