Blogger Information
Blog 46
fans 0
comment 0
visits 34396
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP中的视图基类的实例演示
上草一方
Original
447 people have browsed it

视图类代码如下:

  1. <?php
  2. // 视图类
  3. namespace phpcn;
  4. class view
  5. {
  6. //约定:控制器方法的模板,就是以控制器为目录名,以方法为文件名
  7. protected $controller;
  8. protected $action;
  9. protected $path;
  10. // 模板变量容器
  11. protected $data = [];
  12. // 初始化时创建模板的路径
  13. public function __construct($controller,$action,$path = '/view/')
  14. {
  15. $this->controller = $controller;
  16. $this->action = $action;
  17. $this->path = $path;
  18. }
  19. // 模板赋值:用assign()方法
  20. // assign()方法:在thinkphp中,assign()方法用于打印数组,该方法的第一个参数是在模板取值时所使用
  21. // 的变量名,第二个参数是要传递的值,语法为“$this->assign('name',$value);”。
  22. public function assign($name,$value)
  23. {
  24. // $name 是外部变量在模板文件中的变量
  25. // $value 是模板变量的值
  26. $this->data[$name] = $value;
  27. }
  28. // 模板渲染:一般用render()方法
  29. // 将模板赋值与模板渲染二合一
  30. public function render($path = '' ,$name = null, $value = null)
  31. {
  32. if ($name && $value) $this->assign($name,$value);
  33. // 展开模板变量数组
  34. extract($this->data);
  35. if (empty($path)) {
  36. // 按约定规则来生成模板文件的路径并加载它
  37. $file = __DIR__ .$this->path . $this->controller . '/' .$this->action . '.php';
  38. } else {
  39. $file = $path;
  40. }
  41. // include $file or die('视图不存在);
  42. file_exists($file) ? include $file : die('视图不存在');
  43. }
  44. }
  45. // 测试
  46. $controller = 'User';
  47. $action = 'hello';
  48. $view = new View($controller,$action);
  49. // 模板赋值:变量
  50. $view->assign('username','朱老师');
  51. $items = [
  52. ['name' =>'手机','price' => 2000],
  53. ['name' =>'电脑','price' => 3800],
  54. ['name' =>'相机','price' => 1800],
  55. ];
  56. $view->assign('items',$items);
  57. // 渲染模板
  58. // $view->render();
  59. // 渲染,赋值二合一
  60. $view->render($path = '','lang',['php','java','python']);

调用的视图控制器和方法代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <h3>User控制器的hello()方法</h3>
  11. <h3>Hello,<?= $username ?></h3>
  12. <ul>
  13. <?php foreach ($items as ['name'=>$name,'price'=>$price]): ?>
  14. <li><?= $name ?> :<?= $price ?></li>
  15. <?php endforeach ?>
  16. </ul>
  17. <ul>
  18. <?php foreach ($lang as $value): ?>
  19. <li><?= $value ?></li>
  20. <?php endforeach ?>
  21. </ul>
  22. </body>
  23. </html>

运行效果:

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post