Smarty中的变量

来源:互联网 发布:一个程序员的职业规划 编辑:程序博客网 时间:2024/06/03 18:41

Smarty中变量分为三类

1、PHP分配的变量

index.php

[php] view plain copy
 print?
  1. require('./include.php');   //加载Smarty初始化  
  2.   
  3. $smarty->assign('name','chuangrain');    //php分配的变量  
  4.   
  5. $smarty->display('index.html');      //显示index.html  
index.html

[html] view plain copy
 print?
  1. <{if $name == 'chuangrain'}><{* Smarty中的函数、属性、注释*}>  
  2. hello,<{$name}>!  
  3. <{else}>  
  4. hello,world!  
  5. <{/if}>  

如果是分配的数组和对象,也可以在smarty中访问

例:数组

[php] view plain copy
 print?
  1. $arr1 = array('赵','钱','孙',array('杨','孔'));  
  2.   
  3. $arr2 = array(  
  4.     'zhou' => '周',  
  5.     'zhang' => '张',  
  6.     'wang' => '王',  
  7.     'li' => array(  
  8.         'chen' => '陈',  
  9.         'xiang' => '向',  
  10.     ),  
  11. );  
  12.   
  13. $smarty->assign('arr1',$arr1);  
  14. $smarty->assign('arr2',$arr2);  

[html] view plain copy
 print?
  1. <{$arr1.0}>  
  2. <{$arr1[1]}>  
  3. <{$arr1.2}>  
  4. <{$arr1.3.0}>  
  5. <{$arr1.3[1]}>  
  6. <br>  
  7. <{$arr2.zhou}>  
  8. <{$arr2.zhang}>  
  9. <{$arr2.wang}>  
  10. <{$arr2.li.chen}>  
  11. <{$arr2.li.xiang}>  
运行结果:

例:对象

[php] view plain copy
 print?
  1. class pic {  
  2.     public $height = '300px';  
  3.     public $width = '200px';  
  4. }  
  5.   
  6. $p = new pic();  
  7.   
  8. $smarty->assign('p',$p);  

[html] view plain copy
 print?
  1. <{$p->height}>  
  2. <{$p->width}>  
运行结果:300px 200px

2、配置文件中的变量

使用配置文件中的变量可以通过两个"#"或者smarty中的保留变量$smarty.config.来调用

info.conf

[plain] view plain copy
 print?
  1. name = "chuangrain"  
  2. age = 20  
  3. address = "四川"  
html

[html] view plain copy
 print?
  1. <{config_load file="info.conf"}><{* 加载配置文件中的info.conf *}>  
  2. name:<{#name#}><br><{* 不能将#name#写为"#name#",否则会当作字符串处理 *}>  
  3. age:<{"`$smarty.config.age`"}><br>  
  4. address:<{#address#}><br>  
运行结果:

3、smarty保留的变量

    3.1、request变量

$smarty.get.变量名,可以获取url中变量的值(用得比较多)

$smarty.post.var     $smarty.cookies.var    $smarty.server.var    $smarty.env.var      $smarty.session.var       $smarty.request.var

    3.2、$smarty.now当前时间

[html] view plain copy
 print?
  1. <{$smarty.now}><{*返回当前时间的时间戳*}><br>  
  2. <{$smarty.now|date_format:"%Y-%m-%d %H:%M:%S"}><{*格式化输出当前时间*}><br>  
   运行结果: 

    3.3、$smarty.const.常量名

$smarty.const.常量名直接调用PHP中的常量名,不需要assign

[php] view plain copy
 print?
  1. define('CONST_STR','chuang');  
[html] view plain copy
 print?
  1. <{$smarty.const.CONST_STR}>  
运行结果:chuang
    3.4、$smarty.capture

1.html

[html] view plain copy
 print?
  1. <a href="#">aaaaaaaaaaaaaaaaaa</a>  

[html] view plain copy
 print?
  1. <{capture name=str}>  
  2. <{include file="1.html"}>  
  3. <{/capture}>  
  4. <{if $smarty.capture.str}>  
  5.     <center><h1><{$smarty.capture.str}></h1><center>  
  6. <{/if}>  
<{capture}><{/capture}>之间的内容会放到一个变量中去,name=“变量名”,默认为default
运行结果:

    3.5、$smarty.config.变量名

$smarty.config.变量名,可以获取配置文件中的变量的值,在 2 中已有使用

    3.6、$smarty.section       $smarty.foreach

用在section和foreach循环遍历中

    3.7、$smarty.template

返回本模板的文件名

0 0