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