3 * @file src/Core/Renderer.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Render\FriendicaSmarty;
13 * @brief This class handles Renderer related functions.
15 class Renderer extends BaseObject
18 * @brief An array of registered template engines ('name'=>'class name')
20 public static $template_engines = [];
23 * @brief An array of instanced template engines ('name'=>'instance')
25 public static $template_engine_instance = [];
28 * @brief An array for all theme-controllable parameters
30 * Mostly unimplemented yet. Only options 'template_engine' and
33 public static $theme = [
37 'force_max_items' => 0,
39 'template_engine' => 'smarty3',
42 private static $ldelim = [
46 private static $rdelim = [
52 * @brief This is our template processor
54 * @param string|FriendicaSmarty $s The string requiring macro substitution or an instance of FriendicaSmarty
55 * @param array $vars key value pairs (search => replace)
57 * @return string substituted string
58 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60 public static function replaceMacros($s, $vars)
62 $stamp1 = microtime(true);
65 // pass $baseurl to all templates
66 $vars['$baseurl'] = System::baseUrl();
67 $t = self::getTemplateEngine();
70 $output = $t->replaceMacros($s, $vars);
71 } catch (Exception $e) {
72 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
76 $a->saveTimestamp($stamp1, "rendering");
82 * @brief Load a given template $s
84 * @param string $s Template to load.
85 * @param string $root Optional.
87 * @return string template.
90 public static function getMarkupTemplate($s, $root = '')
92 $stamp1 = microtime(true);
94 $t = self::getTemplateEngine();
97 $template = $t->getTemplateFile($s, $root);
98 } catch (Exception $e) {
99 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
103 $a->saveTimestamp($stamp1, "file");
109 * @brief Register template engine class
111 * @param string $class
113 public static function registerTemplateEngine($class)
115 $v = get_class_vars($class);
117 if (!empty($v['name']))
120 self::$template_engines[$name] = $class;
122 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
128 * @brief Return template engine instance.
130 * If $name is not defined, return engine defined by theme,
133 * @return ITemplateEngine Template Engine instance
135 public static function getTemplateEngine()
137 $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
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];
143 $class = self::$template_engines[$template_engine];
145 self::$template_engine_instance[$template_engine] = $obj;
150 echo "template engine <tt>$template_engine</tt> is not registered!\n";
155 * @brief Returns the active template engine.
157 * @return string the active template engine
159 public static function getActiveTemplateEngine()
161 return self::$theme['template_engine'];
165 * sets the active template engine
167 * @param string $engine the template engine (default is Smarty3)
169 public static function setActiveTemplateEngine($engine = 'smarty3')
171 self::$theme['template_engine'] = $engine;
175 * Gets the right delimiter for a template engine
181 * @param string $engine The template engine (default is Smarty3)
183 * @return string the right delimiter
185 public static function getTemplateLeftDelimiter($engine = 'smarty3')
187 return self::$ldelim[$engine];
191 * Gets the left delimiter for a template engine
197 * @param string $engine The template engine (default is Smarty3)
199 * @return string the left delimiter
201 public static function getTemplateRightDelimiter($engine = 'smarty3')
203 return self::$rdelim[$engine];