多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 输出变量 当我们在控制器输出模板时, 模板是无法直接调用方法函数中的变量的. 我们需要通过框架内置的输出方法, 将变量赋值到模板当中使用. 看一下下面**错误**的示范 **Index** 控制器内容 ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $string = '这是一个字符串'; $this->display("index"); } } ~~~ 模板 **index.html** 内容 ~~~ 我想调用刚才Action 的 $string 变量 输出变量: <?php echo $string; ?> ~~~ 当我们访问时 +++ / <<< 出错 调用了未定义的变量 $string +++ 可见结果, 模板是无法直接使用控制器内的变量的. 我们需要在控制器方法中 输出模板时将变量赋值到模板中 使用**Action**成员 **v函数** 将变量复制到模板中 再次编辑 **Index** 控制器内容 ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $string = '这是一个字符串'; $this->v("string",$string); $this->v("a",$string); $this->display("index"); } } ~~~ **$this->v(复制后名称,传入变量)** **index.html** 模板内容 再次编辑 ~~~ 我想调用刚才Action 的 $string 变量 输出变量: <?php echo $string; ?> 在增加一个 <?php echo $a; ?> HYPHP 内置标签输出变量 : {$a} ~~~ 再次访问首页 +++ get:/ <<< success 我想调用刚才Action 的 $string 变量 输出变量: 这是一个字符串 在增加一个 这是一个字符串 HYPHP 内置标签输出变量 : 这是一个字符串 +++ 可见 输出内容中 **$a** 以及 **$string** 都变成了 **这是一个字符串** * * * * * ## {$变量名} 输出变量 框架的模板引擎提供了一系列输出标签. ~~~ {$a} 等同于 <?php echo $a; ?> ~~~ ## 输出数组变量 {$数组变量.数组索引} **Index** 控制器内容 ~~~ <?php namespace Action; use HY\Action; class Index extends Action { public function Index(){ $arr =array( 'user'=>'admin', 'pass'=>'123456' ); $this->v('arr',$arr); $this->display("index"); } } ~~~ 模板 **index.html** 内容 ~~~ 我想调用刚才Action 的 $arr 变量 输出变量: {$arr.user} {$arr.pass} ~~~ 当我们访问时 +++ / <<< 出错 我想调用刚才Action 的 $arr 变量 输出变量: admin 123456 +++