smarty学习——变量
变量的处理对于模板来说是比较重要的。
Smarty有几种不同类型的变量. 变量 的类型取决于它的前缀是什么符号(或者被什么符号包围)
Smarty的变量可以直接被输出或者作为函数属性和修饰符(modifiers)的参数,
或者用于内部的条件表达式等等. 如果要输出一个变量,只要用定界符将它括起来就可以。
{$Name} {$Contacts[row].Phone} <body bgcolor="{#bgcolor#}">
一.php分配的变量
调用从PHP分配的变量需在前加"$"符号。
调用模板内的assign函数分配的变量也是这样。
比如前面我们的代码
<?php require_once 'smartyUser.php'; $usermodel=new smartyUser(); $usermodel->assign('name','dalong'); $usermodel->display('user.tpl'); ?> {*smarty demo tempalates *} hello ,{$name}!!! {if $name=="dalong"} you are the first one!!! {else} you are the last one {/if}
使用$name 进行访问即可。
1.关联数组
测试代码如下:
<?php require_once 'smartyUser.php'; $usermodel=new smartyUser(); $usermodel->assign('name','dalong'); $usermodel->assign('userinfo',array('username'=>'dalong','userage'=>20)); $usermodel->display('user.tpl'); ?>
模板文件:
{*smarty demo tempalates *} hello ,{$name}!!! <br> username,{$userinfo.username} <br> userage,{$userinfo.userage} <br> {if $name=="dalong"} you are the first one!!! {else} you are the last one {/if}
显示结果:
2.索引数组
<?php require_once 'smartyUser.php'; $usermodel=new smartyUser(); $usermodel->assign('name','dalong'); $usermodel->assign('userinfo',array('username'=>'dalong','userage'=>20)); $usermodel->assign('namelist',array('dalong','lisi','zhangsan')); $usermodel->display('user.tpl'); ?>
模板文件:
{*smarty demo tempalates *} hello ,{$name}!!! <br> username,{$userinfo.username} <br> userage,{$userinfo.userage} <br> firstname:{$namelist[0]} <br> {if $name=="dalong"} you are the first one!!! {else} you are the last one {/if}
测试结果:
3.对象
进行测试使用的简单对象
<?php class user { var $username; var $userage; } ?> <?php require_once 'smartyUser.php'; require_once 'user.php'; $usermodel=new smartyUser(); $usermodel->assign('name','dalong'); $usermodel->assign('userinfo',array('username'=>'dalong','userage'=>20)); $user=new user(); $user->userage=555; $user->username='dalong'; $usermodel->assign('userobject',$user); $usermodel->assign('namelist',array('dalong','lisi','zhangsan')); $usermodel->display('user.tpl'); ?> 模板文件使用: {*smarty demo tempalates *} hello ,{$name}!!! <br> username,{$userinfo.username} <br> userage,{$userinfo.userage} <br> firstname:{$namelist[0]} <br> userobject demo info <br> userage={$userobject->userage} <br> username={$userobject->username} <br> {if $name=="dalong"} you are the first one!!! {else} you are the last one {/if}
测试结果:
二.配置文件信息;
配置文件中的变量需要通过用两个"#"或者是smarty的保留变量 $smarty.config.来调用(下节将讲到)
第二种语法在变量作为属性值并被引号括住的时候非常有用.
(举个例子
{include file="#includefile#"}
这样#includefile#将被当作字符处理,而不表示配置文件变量,
但可以这样表示
{include file="`$smarty.config.includefile`"}
不要忘了加``)
测试代码如下:
userinfo.conf: userinfoname="dalong" userinfoage=33333 conf.tpl: {config_load file="userinfo.conf"} use # <br> username={#userinfoname#} <br> userage={#userinfoage#} <br> use ***$smarty.config**** <br> username:{$smarty.config.userinfoname} <br> userage:{$smarty.config.userinfoage} php : <?php require_once 'smartyUser.php'; require_once 'user.php'; $usermodel=new smartyUser(); $usermodel->assign('name','dalong'); $usermodel->assign('userinfo',array('username'=>'dalong','userage'=>20)); $user=new user(); $user->userage=555; $user->username='dalong'; $usermodel->assign('userobject',$user); $usermodel->assign('namelist',array('dalong','lisi','zhangsan')); $usermodel->display('conf.tpl'); ?>
三.
{$smarty}
保留变量
{$smarty}
保留变量可以被用于访问一些特殊的模板变量.
以下是全部列表:
1.request 变量 $_GET, $_POST, $_COOKIE, $_SERVER, $_ENV and $_SESSION等 {* display value of page from URL (GET) http://www.domain.com/index.php?page=foo *} {$smarty.get.page} {* display the variable "page" from a form (POST) *} {$smarty.post.page} {* display the value of the cookie "username" *} {$smarty.cookies.username} {* display the server variable "SERVER_NAME" *} {$smarty.server.SERVER_NAME} {* display the system environment variable "PATH" *} {$smarty.env.PATH} {* display the php session variable "id" *} {$smarty.session.id} {* display the variable "username" from merged get/post/cookies/server/env *} {$smarty.request.username} 2.$smarty.now {* use the date_format modifier to show current date and time *} {$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"} 3.$smart.const {$smarty.const._MY_CONST_VAL} 4.$smarty.capture 5.$smarty.config 6.$smarty.section 7.$smarty.foreach 8.$smarty.template
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)