Smarty之assign赋数组append赋值

来源:转载

Smarty之assign赋数组append赋值,有需要的朋友可以参考下。

assign赋数组过程

append赋多个值,成索引数组

<?php/*assign 赋值数组append 连续赋值*/require('../../smarty3/libs/Smarty.class.php');$smarty=new Smarty();$smarty->template_dir='./temp';$smarty->compile_dir='./comp';$user=array('name'=>'sonia','age'=>'100');$smarty->assign('user',$user);/*赋值发生了什么smarty/libs/sysplugins/smarty_interal_data.phppublic function assign($tpl_var, $value = null, $nocache = false) {if (is_array($tpl_var)) {foreach ($tpl_var as $_key => $_val) { if ($_key != '') {$this->tpl_vars[$_key] = new Smarty_Variable($_val, $nocache);就看这一段,assign收到的是数组,其实就是$this->tpl_vars[$_key] =$_val===>foreach($arr as $k =>$v)... $this->tpl_vars[name] =sonia相当于把name当成单个值assign给类内的数组;*/$city=array('a'=>array('b'=>array('c'=>'bk')));/*多维数组*/$smarty->assign('city',$city);/*append连续往某一个标签赋很多值*/$smarty->append('num','2015');$smarty->append('num','2050');/*public function append($tpl_var, $value = null, $merge = false, $nocache = false) {... $this->tpl_vars[$tpl_var]->value[] = $value; ===>>>num[]=2015 num[]=2050 */$smarty->display('temp3.html');?>
temp3.html

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>temp3</title></head><body> <h1>{$user.name}</h1> <h2>{$city.a.b.c}</h2> <h3>{$num[0]}</h3> <h3>{$num[1]}</h3></body></html>



版权声明:本文为博主原创文章,未经博主允许不得转载。


分享给朋友:
您可能感兴趣的文章:
随机阅读: