1+ <?php
2+ /**
3+ * The file contains Templater class
4+ *
5+ * PHP version 5
6+ *
7+ * @category Library
8+ * @package Cherry
9+ * @author Temuri Takalandze <[email protected] > 10+ * @license https://github.com/ABGEO07/cherry-templater/blob/master/LICENSE MIT
11+ * @link https://github.com/ABGEO07/cherry-templater
12+ */
13+
14+ namespace Cherry \Templating ;
15+
16+ use Cherry \HttpUtils \Response ;
17+
18+ /**
19+ * Cherry project templater class
20+ *
21+ * @category Library
22+ * @package Cherry
23+ * @author Temuri Takalandze <[email protected] > 24+ * @license https://github.com/ABGEO07/cherry-templater/blob/master/LICENSE MIT
25+ * @link https://github.com/ABGEO07/cherry-templater
26+ */
27+ class Templater
28+ {
29+ private $ _templatesPath ;
30+
31+ /**
32+ * Templater constructor.
33+ *
34+ * @param string $templatesPath Path to templates.
35+ */
36+ public function __construct ($ templatesPath )
37+ {
38+ $ this ->_templatesPath = $ templatesPath ;
39+ }
40+
41+ /**
42+ * Render given template and return response.
43+ *
44+ * @param string $template Template filename in templates folder.
45+ * @param array $variables Arguments for template.
46+ *
47+ * @return Response
48+ */
49+ public function render ($ template , $ variables = [])
50+ {
51+ if (substr ($ template , -13 ) !== '.templater.php ' ) {
52+ $ template .= '.templater.php ' ;
53+ }
54+
55+ $ templatesPath = $ this ->_templatesPath ;
56+
57+ if (file_exists ($ templatesPath . '/ ' . $ template )) {
58+ //Convert array elements into variables
59+ extract ($ variables , EXTR_OVERWRITE );
60+
61+ //Start Output Buffer session
62+ ob_start ();
63+
64+ // Include Template File
65+ include_once "{$ templatesPath }/ {$ template }" ;
66+
67+ //Get Buffer value
68+ $ content = ob_get_contents ();
69+
70+ //Close Buffer session and clear it
71+ ob_end_clean ();
72+ } else {
73+ $ content = "Template {$ template } not found! " ;
74+ }
75+
76+ //Create new Response object
77+ $ response = new Response ();
78+
79+ return $ response ->sendResponse ($ content );
80+ }
81+ }
0 commit comments