Skip to content

Commit 148f3cd

Browse files
committed
Add support for h2o template system http://www.h2o-template.org/
1 parent 000e2c9 commit 148f3cd

File tree

3 files changed

+109
-2
lines changed

3 files changed

+109
-2
lines changed

README.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ This repository contains custom View classes for the template frameworks listed
2222
* Sugar
2323
* Savant
2424
* Rain
25+
* H2o
2526

2627
To learn how to write your own custom View class, visit the [Slim Framework documentation](https://github.com/codeguy/Slim/wiki/Slim-Framework-Documentation#custom-views).
2728

@@ -57,4 +58,4 @@ Slim is in active development, and test coverage is continually improving.
5758

5859
The Slim Framework for PHP 5 and the additional resources in this repository are released under the MIT public license.
5960

60-
<http://www.slimframework.com/license>
61+
<http://www.slimframework.com/license>

Views/H2oView.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+

Views/README.markdown

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,22 @@ The `HaangaView` custom View class provides support for the Haanga templating sy
7171
));
7272
//Insert your application routes here
7373
Slim::run();
74-
?>
74+
?>
75+
76+
## H2oView
77+
78+
The `H2oView` custom View class provides support for the [H2o templating system](http://www.h2o-template.org) for PHP. You can use the H2oView custom View in your application like this:
79+
80+
<?php
81+
require 'slim/Slim.php';
82+
require 'views/H2oView.php';
83+
84+
H2oView::$h2o_directory = './h2o/';
85+
86+
$app = new Slim(array('view' => new H2oView));
87+
// Insert your application routes here
88+
$app->run();
89+
?>
90+
91+
Refer to the `Views/H2oView.php` file for further documentation.
92+

0 commit comments

Comments
 (0)