Home > Backend Development > PHP Tutorial > Let Kohana use a third-party template engine

Let Kohana use a third-party template engine

WBOY
Release: 2016-07-25 09:10:41
Original
1008 people have browsed it
Kohana's view layer is very simple, with variable data placed in the $_data array. Therefore, by inheriting the Kohana_View class and overriding some methods, you can easily use a third-party template engine as the view layer.
The example here is using Blitz Template. If you want to use Smarty or the like, you can modify it similarly.
The file is under application/classes/stucampus/system/view.php and can be modified, but the modification must correspond to the namespace. As for how to support namespaces, see here
  1. namespace StuCampusSystem;
  2. class View extends Kohana_View
  3. {
  4. /**
  5. * Template Engine
  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. // Override parent
  38. $params = array_merge($kohana_view_data, self::$_global_data);
  39. $output = self::$templateEngine->parse($params);
  40. return $output;
  41. }
  42. /**
  43. * Set the file name of the view
  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. // Initialize the view engine
  55. self::$templateEngine = new Blitz ($this->_file);
  56. return $return;
  57. }
  58. /**
  59. * Parse template
  60. *
  61. * @see Kohana_View::render()
  62. */
  63. public function render($file = NULL)
  64. {
  65. // This method is the same as parent, but The parent does not use static delayed binding, so I have to rewrite it once = =#
  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. }
Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template