]> git.mxchange.org Git - friendica.git/blob - src/Core/Renderer.php
Remove duplicate $baseurl template variable
[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 use Friendica\Render\ITemplateEngine;
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                  $vars Key value pairs (search => replace)
57          *
58          * @return string substituted string
59          * @throws Exception
60          */
61         public static function replaceMacros($s, array $vars = [])
62         {
63                 $stamp1 = microtime(true);
64                 $a = self::getApp();
65
66                 // pass $baseurl to all templates if it isn't set
67                 $vars = array_merge(['$baseurl' => $a->getBaseURL()], $vars);
68
69                 $t = self::getTemplateEngine();
70
71                 try {
72                         $output = $t->replaceMacros($s, $vars);
73                 } catch (Exception $e) {
74                         echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
75                         exit();
76                 }
77
78                 $a->getProfiler()->saveTimestamp($stamp1, "rendering", System::callstack());
79
80                 return $output;
81         }
82
83         /**
84          * @brief Load a given template $s
85          *
86          * @param string $s    Template to load.
87          * @param string $root Optional.
88          *
89          * @return string template.
90          * @throws Exception
91          */
92         public static function getMarkupTemplate($s, $root = '')
93         {
94                 $stamp1 = microtime(true);
95                 $a = self::getApp();
96                 $t = self::getTemplateEngine();
97
98                 try {
99                         $template = $t->getTemplateFile($s, $root);
100                 } catch (Exception $e) {
101                         echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
102                         exit();
103                 }
104
105                 $a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
106
107                 return $template;
108         }
109
110         /**
111          * @brief Register template engine class
112          *
113          * @param string $class
114          */
115         public static function registerTemplateEngine($class)
116         {
117                 $v = get_class_vars($class);
118
119                 if (!empty($v['name']))
120                 {
121                         $name = $v['name'];
122                         self::$template_engines[$name] = $class;
123                 } else {
124                         echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
125                         die();
126                 }
127         }
128
129         /**
130          * @brief Return template engine instance.
131          *
132          * If $name is not defined, return engine defined by theme,
133          * or default
134          *
135          * @return ITemplateEngine Template Engine instance
136          */
137         public static function getTemplateEngine()
138         {
139                 $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
140
141                 if (isset(self::$template_engines[$template_engine])) {
142                         if (isset(self::$template_engine_instance[$template_engine])) {
143                                 return self::$template_engine_instance[$template_engine];
144                         } else {
145                                 $class = self::$template_engines[$template_engine];
146                                 $obj = new $class;
147                                 self::$template_engine_instance[$template_engine] = $obj;
148                                 return $obj;
149                         }
150                 }
151
152                 echo "template engine <tt>$template_engine</tt> is not registered!\n";
153                 exit();
154         }
155
156         /**
157          * @brief Returns the active template engine.
158          *
159          * @return string the active template engine
160          */
161         public static function getActiveTemplateEngine()
162         {
163                 return self::$theme['template_engine'];
164         }
165
166         /**
167          * sets the active template engine
168          *
169          * @param string $engine the template engine (default is Smarty3)
170          */
171         public static function setActiveTemplateEngine($engine = 'smarty3')
172         {
173                 self::$theme['template_engine'] = $engine;
174         }
175
176         /**
177          * Gets the right delimiter for a template engine
178          *
179          * Currently:
180          * Internal = ''
181          * Smarty3 = '{{'
182          *
183          * @param string $engine The template engine (default is Smarty3)
184          *
185          * @return string the right delimiter
186          */
187         public static function getTemplateLeftDelimiter($engine = 'smarty3')
188         {
189                 return self::$ldelim[$engine];
190         }
191
192         /**
193          * Gets the left delimiter for a template engine
194          *
195          * Currently:
196          * Internal = ''
197          * Smarty3 = '}}'
198          *
199          * @param string $engine The template engine (default is Smarty3)
200          *
201          * @return string the left delimiter
202          */
203         public static function getTemplateRightDelimiter($engine = 'smarty3')
204         {
205                 return self::$rdelim[$engine];
206         }
207 }