讓Kohana使用第三方模版引擎

來源:互聯網
上載者:User
Kohana的視圖層很簡單,變數資料放在$_data數組中。所以繼承Kohana_View類並覆寫部分方法,就可以非常輕鬆的使用第三方模版引擎作為視圖層。
這裡的例子是使用Blitz Template,如果要用Smarty之類的也可以類似修改。
檔案在application/classes/stucampus/system/view.php下,可以修改,但是修改要和命名空間對應。至於如何支援命名空間,見這裡
  1. namespace StuCampus\System;
  2. class View extends \Kohana_View
  3. {
  4. /**
  5. * 模版引擎
  6. *
  7. * @var \Blitz
  8. */
  9. protected static $templateEngine = null;
  10. /**
  11. * Returns a new View object. If you do not define the "file" parameter,
  12. * you must call [View::set_filename].
  13. *
  14. * $view = View::factory($file);
  15. *
  16. * @param string view filename
  17. * @param array array of values
  18. * @return View
  19. */
  20. public static function factory($file = NULL, array $data = NULL)
  21. {
  22. return new self($file, $data);
  23. }
  24. /**
  25. * Captures the output that is generated when a view is included.
  26. * The view data will be extracted to make local variables. This method
  27. * is static to prevent object scope resolution.
  28. *
  29. * $output = View::capture($file, $data);
  30. *
  31. * @param string filename
  32. * @param array variables
  33. * @return string
  34. */
  35. protected static function capture($kohana_view_filename, array $kohana_view_data)
  36. {
  37. // 覆寫parent
  38. $params = array_merge($kohana_view_data, self::$_global_data);
  39. $output = self::$templateEngine->parse($params);
  40. return $output;
  41. }
  42. /**
  43. * 設定視圖的檔案名稱
  44. *
  45. * @example $view->set_filename($file);
  46. *
  47. * @param string view filename
  48. * @return View
  49. * @throws \Kohana_View_Exception
  50. */
  51. public function set_filename($file)
  52. {
  53. $return = parent::set_filename($file);
  54. // 初始化試圖引擎
  55. self::$templateEngine = new \Blitz($this->_file);
  56. return $return;
  57. }
  58. /**
  59. * 解析模板
  60. *
  61. * @see Kohana_View::render()
  62. */
  63. public function render($file = NULL)
  64. {
  65. // 這個方法和parent一樣的,但是parent沒有使用靜態延遲綁定,所以只好重寫一次= =#
  66. if ($file !== NULL)
  67. {
  68. $this->set_filename($file);
  69. }
  70. if (empty($this->_file))
  71. {
  72. throw new Kohana_View_Exception('You must set the file to use within your view before rendering');
  73. }
  74. // Combine local and global data and capture the output
  75. return View::capture($this->_file, $this->_data);
  76. }
  77. }
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.