模板文件中分配的变量

来源:互联网 发布:针式打印机模板软件 编辑:程序博客网 时间:2024/05/18 00:18

模板文件中变量的分配:

一、模板中的注释

模板注释被*号包围,例如 <{* this is a comment *}>

二、从php文件中分配过来的变量

要从php中分配变量,要用对象指向模板中的assign()函数。

例如:$smarty->assign("title","hello world");

在模板文件中你想显示的地方写<{$title}>即可显示出来hello world。

三、分配数组

(1)自定义的索引数组

$tpl->assign("array1",array("1","2","3"));
<!--{ $array1[0] }--><br>
<!--{ $array1[1] }--><br>
<!--{ $array1[2] }--><br>

(2)自定义的关联数组

$tpl->assign("array3",array("one"=>"one","two"=>"two"));
分配自定义数组array3 (关联数组,用数组名点上“.”元素下标名称)
<!--{ $array3.one }--><br>
<!--{ $array3.two }--><br>

(3)关联数组和索引数组混合

$tpl->assign("array4",array(array("one"=>"one"),"two"=>array("two"),array("three"=>"three")));
分配自定义数组array4 (数组中的数组的分配,(数组中的数组可为关联,可为索引,注意语法结构))
<!--{ $array4[0].one }--><br>
<!--{ $array4.two[0] }--><br>
<!--{ $array4[1].three }--><br>

四、从对象中获取变量

class Person{
        public $name;
        public $age;
       
        function __construct($name,$age){
               $this->name=$name;
               $this->age=$age;
        }
       
        function say(){
               return $this->name."年龄是:".$this->age;
        }

}

调用对象assign()函数分配变量

$tpl->assign("person1", new Person("lisi",12));
<!--{ $person1->name }-->
<!--{ $person1->say() }-->
五、数学运算
$tpl->assign("num1",10);
$tpl->assign("num2",20);
 
<!--{ $num1 }-->
<!--{ $num2 }-->
<!--{ $num2*num2 }-->
<!--{ $num2/num2 }-->

注意:

Smarty3.1.4版本里

关联数组和索引数组一样可以使用[]

  $array1[‘one’]

Smarty2.6.26版本里

关联数组:使用.连接下标;

索引数组:使用[]连接下标;

 

原创粉丝点击