1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Castor\Helpers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Castor\Facades\Development as DevelopmentFacade; |
6
|
|
|
use GeminiLabs\Castor\Facades\Log as LogFacade; |
7
|
|
|
use GeminiLabs\Castor\Facades\Utility as UtilityFacade; |
8
|
|
|
|
9
|
|
|
class Template |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
public $template; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param string $slug |
18
|
|
|
* @param string $name |
19
|
|
|
* |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function get($slug, $name = '') |
23
|
|
|
{ |
24
|
|
|
$template = UtilityFacade::startWith('templates/', $slug); |
25
|
|
|
$templates = ["$template.php"]; |
26
|
|
|
if (!empty($name)) { |
27
|
|
|
$fileName = basename($template); |
28
|
|
|
$filePath = UtilityFacade::trimRight($template, $fileName); |
29
|
|
|
array_unshift($templates, sprintf('%s/%s.php', $filePath.$name, $fileName)); |
30
|
|
|
} |
31
|
|
|
$templates = array_unique(apply_filters("castor/templates/$slug", $templates, $name)); |
32
|
|
|
$template = locate_template($templates); |
33
|
|
|
if (empty($template)) { |
34
|
|
|
if (file_exists("$slug.php")) { |
35
|
|
|
return "$slug.php"; |
36
|
|
|
} |
37
|
|
|
LogFacade::debug("$slug not found."); |
38
|
|
|
} |
39
|
|
|
return $template; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $slug |
44
|
|
|
* @param string $name |
45
|
|
|
* |
46
|
|
|
* @return void |
47
|
|
|
*/ |
48
|
|
|
public function load($slug, $name = '') |
49
|
|
|
{ |
50
|
|
|
if (!empty(($template = $this->get($slug, $name)))) { |
51
|
|
|
DevelopmentFacade::storeTemplatePath($template); |
52
|
|
|
load_template($template, false); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return void |
58
|
|
|
*/ |
59
|
|
|
public function main() |
60
|
|
|
{ |
61
|
|
|
$this->load($this->template); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $template |
66
|
|
|
* |
67
|
|
|
* @return string|void |
68
|
|
|
*/ |
69
|
|
|
public function setLayout($template) |
70
|
|
|
{ |
71
|
|
|
$template = str_replace(get_stylesheet_directory().'/templates/', '', $template); |
72
|
|
|
$this->template = UtilityFacade::trimRight($template, '.php'); |
73
|
|
|
return $this->get(apply_filters('castor/templates/layout', 'layouts/default')); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|