-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwp-haml.php
More file actions
211 lines (165 loc) · 5.46 KB
/
wp-haml.php
File metadata and controls
211 lines (165 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/*
Plugin Name: WP-HAML
Plugin URI: http://thedextrousweb.com/wp-haml
Description: Allows you to write Wordpress themes using HAML
Author: Harry Metcalfe
Version: 1.0
Author URI: http://thedextrousweb.com
This plugin allows you to write your Wordpress theme templates using HAML instead of a mish-mash of HTML and PHP.
It overrides Wordpress's template loader and uses <a href="http://wphaml.sourceforge.net/">wphaml</a> to parse the HAML
and emit the results.
See the README in the plugin directory for more information.
*/
/*
* Config
*/
define('COMPILED_TEMPLATES', WP_CONTENT_DIR . '/compiled-templates');
/*
* Setup and teardown
*/
register_activation_hook(__FILE__, 'wphaml_activate');
register_deactivation_hook(__FILE__, 'wphaml_deactivate');
function wphaml_activate()
{
if(!file_exists(COMPILED_TEMPLATES) && !mkdir(COMPILED_TEMPLATES))
{
add_action('admin_notices', 'wphaml_warning');
}
if (!defined('HAML_TEMPLATES')) {
define('HAML_TEMPLATES', wph_theme_dir() . '/templates/');
}
if(!file_exists(HAML_TEMPLATES) && !mkdir(HAML_TEMPLATES))
{
add_action('admin_notices', 'wphaml_dir_warning');
}
}
function wphaml_deactivate()
{
}
function wphaml_warning()
{
echo "<div class='updated fade'><p>In order for php-haml to work you need to create <em>" . COMPILED_TEMPLATES . "</em> and make sure it's writeable by your webserver</p></div>";
}
function wphaml_dir_warning()
{
echo "<div class='updated fade'><p>In order for php-haml to work you need to create <em>" . HAML_TEMPLATES . "</em> and make sure it's writeable by your webserver</p></div>";
}
/*
* Template handling
*/
require_once dirname(__FILE__) . "/helpers.php";
require_once dirname(__FILE__) . '/hamlphp/src/HamlPHP/HamlPHP.php';
require_once dirname(__FILE__) . '/hamlphp/src/HamlPHP/Storage/FileStorage.php';
/**
* $template_layout is set by the template if it wishes to use a custom layout.
*
* The loader compiles and executes the template, saves its output to $template_output,
* and then compiles and executes the layout. The layout calls yield() to include the
* content of the template.
*/
$template_layout = $template_output = '';
/**
* Intercepts template includes using our new filter and looks for a HAML alternative.
*/
add_filter('template_include', 'wphaml_template_include');
function wphaml_template_include($template)
{
// Globalise the Wordpress environment
global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;
// Globalise the stuff we need
global $template_output, $template_layout;
$haml_template = wph_haml_template_dir();
// Is there a haml template?
if($template == '')
{
$haml_template .= 'index.haml';
}
else if(substr($template, -5) == '.haml')
{
$haml_template .= $template;
}
else
{
$haml_template .= $template.'.haml';
}
if(file_exists($haml_template))
{
// Execute the template and save its output
$template_output = wphaml_get_parsed_result($haml_template);
/*
if($template_layout == '')
{
$template_layout = wph_haml_template_dir().'layout.haml';
}
// Execute the layout and display everything
echo wphaml_get_parsed_result($template_layout);
*/
echo $template_output;
return null;
}
return $template;
}
function wphaml_get_parsed_result($template)
{
// Make sure that a directory _tmp_ exists in your application and it is writable.
$parser = new HamlPHP(new FileStorage('/projects/SiteNinja/base-site/wp-content/themes/sn-base-theme/cache/'));
$content = $parser->parseFile($template);
return $content;
}
/*
* Create haml alternatives for the get_* functions
*/
function use_layout($name)
{
global $template_layout;
$layout = TEMPLATEPATH . "/layout-$name.haml";
if(!file_exists($layout))
{
trigger_error("The specified layout could not be found: <em>$layout</em>", E_USER_ERROR);
die();
}
$template_layout = $layout;
}
function render_partial($name, $return = false)
{
$partial_template = wph_haml_template_dir() . "partials/_$name.haml";
if(!file_exists($partial_template))
{
trigger_error("The specified partial could not be found: <em>$partial_template</em>", E_USER_ERROR);
die();
}
// Execute the template and save its output
$parser = new HamlParser(wph_haml_template_dir(), COMPILED_TEMPLATES);
$parser->setFile($partial_template);
$partial_output = $parser->render();
if($return)
{
return $partial_output;
}
echo $partial_output;
}
function yield()
{
global $template_output;
if($template_output == '')
{
trigger_error("<tt>yield</tt> had no output to emit (\$template_output is empty). Did your template do anything?", E_USER_NOTICE);
die();
}
echo $template_output;
}
/*
* Warn people not to use get_header and get_footer
*/
/*
add_action('get_header', 'wphaml_headfoot_warnings');
add_action('get_footer', 'wphaml_headfoot_warnings');
add_action('get_sidebar', 'wphaml_headfoot_warnings');
add_action('get_search_form', 'wphaml_headfoot_warnings');
function wphaml_headfoot_warnings()
{
trigger_error("Eek! Don't use get_header, get_footer, get_sidebar or get_search_form. You should use layouts and partials instead: <tt>use_layout</tt> and <tt>get_partial</tt>", E_USER_WARNING);
}
*/
?>