变量的处理对于模板来说是比较重要的。
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