]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
Template Engine and props to Renderer
[friendica.git] / src / Core / Renderer.php
1 <?php
2 /**
3  * @file src/Core/Renderer.php
4  */
5
6 namespace Friendica\Core;
7
8 use Exception;
9 use Friendica\BaseObject;
10 use Friendica\Core\System;
11 use Friendica\Render\FriendicaSmarty;
12
13 /**
14  * @brief This class handles Renderer related functions.
15  */
16 class Renderer extends BaseObject
17 {
18     /**
19          * @brief An array of registered template engines ('name'=>'class name')
20          */
21     public static $template_engines = [];
22
23     /**
24          * @brief An array of instanced template engines ('name'=>'instance')
25          */
26         public static $template_engine_instance = [];
27
28     /**
29          * @brief An array for all theme-controllable parameters
30          *
31          * Mostly unimplemented yet. Only options 'template_engine' and
32          * beyond are used.
33          */
34         public static $theme = [
35                 'sourcename' => '',
36                 'videowidth' => 425,
37                 'videoheight' => 350,
38                 'force_max_items' => 0,
39                 'stylesheet' => '',
40                 'template_engine' => 'smarty3',
41         ];
42     
43     private static $ldelim = [
44                 'internal' => '',
45                 'smarty3' => '{{'
46         ];
47         private static $rdelim = [
48                 'internal' => '',
49                 'smarty3' => '}}'
50     ];
51     
52     /**
53      * @brief This is our template processor
54      *
55      * @param string|FriendicaSmarty $s The string requiring macro substitution or an instance of FriendicaSmarty
56      * @param array $r                  key value pairs (search => replace)
57      * 
58      * @return string substituted string
59     */
60     public static function replaceMacros($s, $r)
61     {
62         $stamp1 = microtime(true);
63         $a = self::getApp();
64
65         // pass $baseurl to all templates
66         $r['$baseurl'] = System::baseUrl();
67         $t = self::getTemplateEngine();
68
69         try {
70             $output = $t->replaceMacros($s, $r);
71         } catch (Exception $e) {
72             echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
73             killme();
74         }
75
76         $a->saveTimestamp($stamp1, "rendering");
77
78         return $output;
79     }
80
81     /**
82      * @brief Load a given template $s
83      *
84      * @param string $s     Template to load.
85      * @param string $root  Optional.
86      * 
87      * @return string template.
88      */
89     public static function getMarkupTemplate($s, $root = '')
90     {
91         $stamp1 = microtime(true);
92         $a = self::getApp();
93         $t = self::getTemplateEngine();
94
95         try {
96             $template = $t->getTemplateFile($s, $root);
97         } catch (Exception $e) {
98             echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
99             killme();
100         }
101
102         $a->saveTimestamp($stamp1, "file");
103
104         return $template;
105     }
106
107     /**
108          * @brief Register template engine class
109          *
110          * @param string $class
111          */
112         public static function registerTemplateEngine($class)
113         {
114         $v = get_class_vars($class);
115         
116         if (!empty($v['name']))
117         {
118                         $name = $v['name'];
119                         self::$template_engines[$name] = $class;
120                 } else {
121                         echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
122                         die();
123                 }
124         }
125
126         /**
127          * @brief Return template engine instance.
128          *
129          * If $name is not defined, return engine defined by theme,
130          * or default
131          *
132          * @return object Template Engine instance
133          */
134         public static function getTemplateEngine()
135         {
136                 $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
137
138                 if (isset(self::$template_engines[$template_engine])) {
139                         if (isset(self::$template_engine_instance[$template_engine])) {
140                                 return self::$template_engine_instance[$template_engine];
141                         } else {
142                                 $class = self::$template_engines[$template_engine];
143                                 $obj = new $class;
144                                 self::$template_engine_instance[$template_engine] = $obj;
145                                 return $obj;
146                         }
147                 }
148
149                 echo "template engine <tt>$template_engine</tt> is not registered!\n";
150                 exit();
151     }
152     
153     /**
154          * @brief Returns the active template engine.
155          *
156          * @return string the active template engine
157          */
158         public static function getActiveTemplateEngine()
159         {
160                 return self::$theme['template_engine'];
161         }
162
163         /**
164          * sets the active template engine
165          *
166          * @param string $engine the template engine (default is Smarty3)
167          */
168         public static function setActiveTemplateEngine($engine = 'smarty3')
169         {
170                 self::$theme['template_engine'] = $engine;
171         }
172
173     /**
174          * Gets the right delimiter for a template engine
175          *
176          * Currently:
177          * Internal = ''
178          * Smarty3 = '{{'
179          *
180          * @param string $engine The template engine (default is Smarty3)
181          *
182          * @return string the right delimiter
183          */
184         public static function getTemplateLeftDelimiter($engine = 'smarty3')
185         {
186                 return self::$ldelim[$engine];
187         }
188
189         /**
190          * Gets the left delimiter for a template engine
191          *
192          * Currently:
193          * Internal = ''
194          * Smarty3 = '}}'
195          *
196          * @param string $engine The template engine (default is Smarty3)
197          *
198          * @return string the left delimiter
199          */
200         public static function getTemplateRightDelimiter($engine = 'smarty3')
201         {
202                 return self::$rdelim[$engine];
203         }
204 }