-
Notifications
You must be signed in to change notification settings - Fork 3
/
templateloader.sublime-snippet
202 lines (175 loc) · 4.99 KB
/
templateloader.sublime-snippet
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
<snippet>
<content><![CDATA[
/**
* $4 Template Loader
*
* @since NEXT
* @package $4
*/
/**
* $4 Template Loader.
*
* @since NEXT
*/
class $1_Template_Loader {
/**
* Array of arguments for template
*
* @var array
* @since NEXT
*/
public \$args = array();
/**
* Template names array
*
* @var array
* @since NEXT
*/
public \$templates = array();
/**
* Template name
*
* @var string
* @since NEXT
*/
public \$template = '';
/**
* HTML view template loader constructor.
*
* @param string \$template The template file name, relative to the includes/templates/ folder - with or without .php extension
* @param string \$name The name of the specialised template. If array, will take the place of the \$args.
* @param array \$args An array of arguments to extract as variables into the template
*
* @return void
*/
public function __construct( \$template, \$name = null, array \$args = array() ) {
if ( empty( \$template ) ) {
throw new Exception( 'Template variable required for '. __CLASS__ .'.' );
}
\$templates = array();
if ( is_array( \$name ) ) {
\$this->args = \$name;
} else {
\$this->args = \$args;
\$name = (string) \$name;
if ( '' !== \$name ) {
\$this->templates[] = "{\$template}-{\$name}.php";
}
}
\$this->templates[] = "{\$template}.php";
}
/**
* Loads the view and outputs it
*
* @since NEXT
*
* @param boolean \$echo Whether to output or return the template
*
* @return string Rendered template
*/
public function load( \$echo = false ) {
\$template = \$this->locate_template();
// Filter args before outputting template.
\$this->args = apply_filters( "template_attributes_for_{\$this->template}", \$this->args );
try {
ob_start();
// Do html
include \$template;
// grab the data from the output buffer and add it to our \$content variable
\$content = ob_get_clean();
} catch ( Exception \$e ) {
wpdie( \$e->getMessage() );
}
if ( ! \$echo ) {
return \$content;
}
echo \$content;
}
/**
* Retrieve the name of the highest priority template file that exists.
*
* Searches in the STYLESHEETPATH before TEMPLATEPATH and then this plugin's /templates
* so that themes which inherit from a parent theme can just overload one file.
*
* @since NEXT
*
* @return string The located template filename.
*/
protected function locate_template() {
\$located = '';
foreach ( \$this->templates as \$template ) {
if ( \$located = \$this->_locate( \$template ) ) {
\$this->template = \$template;
return \$located;
}
}
return \$located;
}
/**
* Searches for template in 1) child theme, 2) parent theme, 3) this plugin.
*
* @since NEXT
*
* @param string \$template Template file to search for.
*
* @return void
*/
protected function _locate( \$template ) {
\$theme_file_path = '/$2/' . \$template;
\$located = '';
if ( file_exists( STYLESHEETPATH . \$theme_file_path ) ) {
\$located = STYLESHEETPATH . \$theme_file_path;
} elseif ( file_exists( TEMPLATEPATH . \$theme_file_path ) ) {
\$located = TEMPLATEPATH . \$theme_file_path;
} elseif(
( \$look = $3::dir( 'templates/' . \$template ) )
&& file_exists( \$look )
) {
\$located = \$look;
}
return \$located;
}
public function get( \$arg, \$default = null ) {
if ( isset( \$this->args[ \$arg ] ) ) {
return \$this->args[ \$arg ];
}
return \$default;
}
public function output( \$arg, \$esc_cb = '', \$default = null ) {
\$val = \$this->get( \$arg, \$default );
echo \$esc_cb ? \$esc_cb( \$val ) : \$val;
}
public function __toString() {
return \$this->load();
}
/**
* Render an HTML view with the given arguments and return the view's contents.
*
* @param string \$template The template file name, relative to the includes/templates/ folder - with or without .php extension
* @param string \$name The name of the specialised template. If array, will take the place of the \$args.
* @param array \$args An array of arguments to extract as variables into the template
*
* @return string Rendered template output
*/
public static function get_template( \$template, \$name = null, array \$args = array() ) {
\$view = new $1_Template_Loader( \$template, \$name, \$args );
return \$view->load();
}
/**
* Render an HTML view with the given arguments and output the view's contents.
*
* @param string \$template The template file name, relative to the includes/templates/ folder - with or without .php extension
* @param string \$name The name of the specialised template. If array, will take the place of the \$args.
* @param array \$args An array of arguments to extract as variables into the template
*
* @return void
*/
public static function output_template( \$template, \$name = null, array \$args = array() ) {
\$view = new $1_Template_Loader( \$template, \$name, \$args );
\$view->load( 1 );
}
}
$0
]]></content>
<tabTrigger>templateloader</tabTrigger>
</snippet>