|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Slim - a micro PHP 5 framework |
| 4 | + * |
| 5 | + * @author Josh Lockhart |
| 6 | + * @link http://www.slimframework.com |
| 7 | + * @copyright 2011 Josh Lockhart |
| 8 | + * |
| 9 | + * MIT LICENSE |
| 10 | + * |
| 11 | + * Permission is hereby granted, free of charge, to any person obtaining |
| 12 | + * a copy of this software and associated documentation files (the |
| 13 | + * "Software"), to deal in the Software without restriction, including |
| 14 | + * without limitation the rights to use, copy, modify, merge, publish, |
| 15 | + * distribute, sublicense, and/or sell copies of the Software, and to |
| 16 | + * permit persons to whom the Software is furnished to do so, subject to |
| 17 | + * the following conditions: |
| 18 | + * |
| 19 | + * The above copyright notice and this permission notice shall be |
| 20 | + * included in all copies or substantial portions of the Software. |
| 21 | + * |
| 22 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 23 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 24 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 25 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 26 | + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 27 | + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 28 | + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * H2oView |
| 33 | + * |
| 34 | + * The H2oView is a custom View class which provides support for the H2o templating system (http://www.h2o-template.org). |
| 35 | + * |
| 36 | + * @package Slim |
| 37 | + * @author Cenan Ozen <http://cenanozen.com/> |
| 38 | + */ |
| 39 | +class H2oView extends Slim_View { |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string The path to the h2o.php WITH a trailing slash |
| 43 | + */ |
| 44 | + public static $h2o_directory = ''; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var array H2o options, see H2o documentation for reference |
| 48 | + */ |
| 49 | + public static $h2o_options = array(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Renders a template using h2o |
| 53 | + * |
| 54 | + * @param string $template template file name |
| 55 | + * @return string |
| 56 | + */ |
| 57 | + public function render($template) { |
| 58 | + if ( ! array_key_exists('searchpath', self::$h2o_options)) { |
| 59 | + self::$h2o_options['searchpath'] = $this->getTemplatesDirectory().'/'; |
| 60 | + } |
| 61 | + |
| 62 | + // Make sure H2o is loaded |
| 63 | + $this->_load_h2o(); |
| 64 | + |
| 65 | + $h2o = new H2o($template, self::$h2o_options); |
| 66 | + return $h2o->render($this->data); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Loads H2o library if it is not already loaded |
| 71 | + * |
| 72 | + * @access private |
| 73 | + * @throws RuntimeException if h2o directory doesn't exist |
| 74 | + * @return void |
| 75 | + */ |
| 76 | + private function _load_h2o() { |
| 77 | + if (class_exists('H2o')) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if ( ! is_dir(self::$h2o_directory)) { |
| 82 | + throw new RuntimeException('h2o directory is invalid'); |
| 83 | + } |
| 84 | + require_once self::$h2o_directory . 'h2o.php'; |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
0 commit comments