Let Kohana use a third-party template engine
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
- namespace StuCampusSystem;
- class View extends Kohana_View
- {
- /**
- * Template Engine
- *
- * @var Blitz
- */
- protected static $templateEngine = null;
- /**
- * Returns a new View object. If you do not define the "file" parameter,
- * you must call [View::set_filename].
- *
- * $view = View::factory($file);
- *
- * @param string view filename
- * @param array array of values
- * @return View
- */
- public static function factory($file = NULL, array $data = NULL)
- {
- return new self($file, $data);
- }
- /**
- * Captures the output that is generated when a view is included.
- * The view data will be extracted to make local variables. This method
- * is static to prevent object scope resolution.
- *
- * $output = View::capture($file, $data);
- *
- * @param string filename
- * @param array variables
- * @return string
- */
- protected static function capture($kohana_view_filename, array $ kohana_view_data)
- {
- // Override parent
- $params = array_merge($kohana_view_data, self::$_global_data);
- $output = self::$templateEngine->parse($params);
- return $output;
- }
-
- /**
- * Set the file name of the view
- *
- * @example $view->set_filename($file);
- *
- * @param string view filename
- * @return View
- * @throws Kohana_View_Exception
- */
- public function set_filename($file)
- {
- $return = parent::set_filename($file);
-
- // Initialize the view engine
- self::$templateEngine = new Blitz ($this->_file);
-
- return $return;
- }
-
- /**
- * Parse template
- *
- * @see Kohana_View::render()
- */
- public function render($file = NULL)
- {
- // This method is the same as parent, but The parent does not use static delayed binding, so I have to rewrite it once = =#
- if ($file !== NULL)
- {
- $this->set_filename($file);
- }
-
- if (empty($this ->_file))
- {
- throw new Kohana_View_Exception('You must set the file to use within your view before rendering');
- }
-
- // Combine local and global data and capture the output
- return View::capture ($this->_file, $this->_data);
- }
-
- }
Copy code
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31