1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class templater engine |
4
|
|
|
* This class will use twig as default and do frequently works for us. |
5
|
|
|
* |
6
|
|
|
* Text Domain: va-templater |
7
|
|
|
* |
8
|
|
|
* Usage: |
9
|
|
|
* $template = new VA\Templater($path); |
10
|
|
|
*/ |
11
|
|
|
namespace VA; |
12
|
|
|
|
13
|
|
|
use \Twig_Loader_Filesystem; |
14
|
|
|
use \Twig_Environment; |
15
|
|
|
use Philo\Blade\Blade; |
16
|
|
|
|
17
|
|
|
class Templater { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Template adapter, it would be twig or any php template lib. |
21
|
|
|
* |
22
|
|
|
* @var $adapter object Template Engine object |
23
|
|
|
*/ |
24
|
|
|
private $adapter = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Template adapter, it would be twig or any php template lib. |
28
|
|
|
* |
29
|
|
|
* @var $adapter object Template Engine object |
30
|
|
|
*/ |
31
|
|
|
private $dir_path; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Template adapter name, it would be twig or any php template lib. |
35
|
|
|
* |
36
|
|
|
* @var $adapter object Template Engine object |
37
|
|
|
*/ |
38
|
|
|
private $adapter_name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Initialize function |
42
|
|
|
* - Load twig as default engine |
43
|
|
|
* |
44
|
|
|
* @var $dir_path string Path to template folder. |
45
|
|
|
*/ |
46
|
|
|
public function __construct($dir_path, $adapter_name = 'blade') { |
47
|
|
|
$this->adapter_name = $adapter_name; |
48
|
|
|
$this->dir_path = $dir_path; |
49
|
|
|
$this->loadAdapter($adapter_name); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Load Twig as default adapter |
54
|
|
|
* |
55
|
|
|
* @return Twig_Environment |
56
|
|
|
*/ |
57
|
|
|
public function loadTwig() { |
58
|
|
|
if(!$this->adapter) { |
|
|
|
|
59
|
|
|
$loader = new Twig_Loader_Filesystem($this->dir_path); |
60
|
|
|
$this->adapter = new Twig_Environment($loader, array( |
|
|
|
|
61
|
|
|
|
62
|
|
|
)); |
63
|
|
|
} |
64
|
|
|
return $this->adapter; |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Load Blade |
69
|
|
|
* |
70
|
|
|
* @return Blade |
71
|
|
|
*/ |
72
|
|
|
public function loadBlade() { |
73
|
|
|
$this->adapter = new Blade($this->dir_path,'/tmp');//second parameter is where cache view is located. |
|
|
|
|
74
|
|
|
return $this->adapter; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Add custom functions for Twig |
79
|
|
|
* |
80
|
|
|
* @param string |
81
|
|
|
* @param function |
82
|
|
|
* |
83
|
|
|
* @return Blade |
84
|
|
|
*/ |
85
|
|
|
public function addFunction($functionName, $callback) { |
86
|
|
|
if($this->adapter_name == 'twig') { |
87
|
|
|
$this->adapter->addFunction(new \Twig_SimpleFunction($functionName, $callback)); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Load adapter, currently we are using 2 adapters: twig and blade |
94
|
|
|
* |
95
|
|
|
* @var $adapter_name string Path to template folder. |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function loadAdapter($adapter_name) { |
99
|
|
|
if(!$this->adapter) { // |
|
|
|
|
100
|
|
|
$method_name = 'load'.ucfirst($adapter_name); |
101
|
|
|
if(method_exists($this, $method_name)) { |
102
|
|
|
$this->$method_name(); |
103
|
|
|
}else{ |
104
|
|
|
throw new \Exception("Could not load adapter $adapter_name"); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return $this->adapter; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Alias for engine render function |
112
|
|
|
* |
113
|
|
|
* @var $template string Template file name (e.g. test.php, folder/test.phtml...) |
114
|
|
|
* @var $data array Data array. E.g. ['list' => $list] |
115
|
|
|
* |
116
|
|
|
* @return string Rendered HTML/text |
117
|
|
|
*/ |
118
|
|
|
public function render($template, $data = []) { |
119
|
|
|
if($this->adapter_name == 'twig') |
120
|
|
|
{ |
121
|
|
|
return $this->adapter->render($template, $data); |
|
|
|
|
122
|
|
|
} |
123
|
|
|
elseif($this->adapter_name == 'blade') |
124
|
|
|
{ |
125
|
|
|
return $this->adapter->view()->make($template,$data)->render(); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Setup Wordpress themes support. |
131
|
|
|
* The template engine will find template file in theme folder before find it in plugin/application folder |
132
|
|
|
* 1. /wp-content/themes/{theme-name}/templates/{application-name}/ |
133
|
|
|
* 2. /path/to/application/ |
134
|
|
|
* |
135
|
|
|
* @var $app_name string Your application name, should be lowercase, letters only. |
136
|
|
|
* |
137
|
|
|
* @return object \VA\Templater\Engine |
138
|
|
|
*/ |
139
|
|
|
public function setWordPressThemeSupport($app_name) { |
140
|
|
|
// Path to templates folder in wordpress theme |
141
|
|
|
$theme_template_path = get_template_directory() . '/templates/'. $app_name; |
142
|
|
|
|
143
|
|
|
// Create folder path if it does not exists. |
144
|
|
|
if(!file_exists($theme_template_path)) { |
145
|
|
|
wp_mkdir_p($theme_template_path); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// Preapare template folders |
149
|
|
|
$template_folders = [ |
150
|
|
|
$theme_template_path, // Load this path first |
151
|
|
|
$this->dir_path // Load this path second |
152
|
|
|
]; |
153
|
|
|
$loader = new \Twig_Loader_Filesystem($template_folders); |
154
|
|
|
$this->adapter->setLoader($loader); |
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.