-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtemplate.php
More file actions
80 lines (70 loc) · 1.87 KB
/
Copy pathtemplate.php
File metadata and controls
80 lines (70 loc) · 1.87 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
<?php
/**
* A simple HTML view renderer.
*/
class Prompt_Template {
/** @var string */
protected $dir;
/** @var string */
protected $name;
/**
* Instantiate an HTML template.
*
* @since 2.0.0
* @param string $name
* @param string|null $dir
*/
public function __construct( $name, $dir = null ) {
$this->name = $name;
$this->dir = $dir ? $dir : path_join( Prompt_Core::$dir_path, 'templates/html' );
}
/**
* Search the active theme for a template, falling back to the plugin template.
*
* Templates are are sought first in a 'prompt' subdirectory, then the theme
* root. If none are found, the plugin default is used.
*
* @return string Selected template.
*/
public function locate() {
if ( file_exists( $this->name ) ) {
return $this->name;
}
// First choice is a template in the theme root or prompt subdirectory
$template_names = array(
'postmatic/' . $this->name,
'prompt/' . $this->name,
$this->name,
);
$template = locate_template( $template_names );
// For local mailing look for an inlined template first
if ( ! $template and ! Prompt_Core::is_api_transport() ) {
$template = path_join( $this->dir . '-inlined', $this->name );
}
// Fallback is the core or provided directory
if ( ! $template or ! file_exists( $template ) ) {
$template = path_join( $this->dir, $this->name );
}
return $template;
}
/**
* Render a template with an array of data in scope.
*
* @param array $data An array of data to provide to the template
* @param boolean $echo Whether to echo output, default false
* @return string Rendered output
*/
public function render( $data = array(), $echo = false ) {
$output = '';
$template = $this->locate();
if ( $template ) {
extract( $data );
if ( !$echo )
ob_start();
require( $template );
if ( !$echo )
$output = ob_get_clean();
}
return $output;
}
}