3 * @file src/Core/Renderer.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Render\FriendicaSmarty;
11 use Friendica\Render\ITemplateEngine;
14 * @brief This class handles Renderer related functions.
16 class Renderer extends BaseObject
19 * @brief An array of registered template engines ('name'=>'class name')
21 public static $template_engines = [];
24 * @brief An array of instanced template engines ('name'=>'instance')
26 public static $template_engine_instance = [];
29 * @brief An array for all theme-controllable parameters
31 * Mostly unimplemented yet. Only options 'template_engine' and
34 public static $theme = [
38 'force_max_items' => 0,
40 'template_engine' => 'smarty3',
43 private static $ldelim = [
47 private static $rdelim = [
53 * @brief This is our template processor
55 * @param string|FriendicaSmarty $s The string requiring macro substitution or an instance of FriendicaSmarty
56 * @param array $vars Key value pairs (search => replace)
58 * @return string substituted string
61 public static function replaceMacros($s, array $vars = [])
63 $stamp1 = microtime(true);
66 // pass $baseurl to all templates if it isn't set
67 $vars = array_merge(['$baseurl' => $a->getBaseURL()], $vars);
69 $t = self::getTemplateEngine();
72 $output = $t->replaceMacros($s, $vars);
73 } catch (Exception $e) {
74 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
78 $a->getProfiler()->saveTimestamp($stamp1, "rendering", System::callstack());
84 * @brief Load a given template $s
86 * @param string $s Template to load.
87 * @param string $root Optional.
89 * @return string template.
92 public static function getMarkupTemplate($s, $root = '')
94 $stamp1 = microtime(true);
96 $t = self::getTemplateEngine();
99 $template = $t->getTemplateFile($s, $root);
100 } catch (Exception $e) {
101 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
105 $a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
111 * @brief Register template engine class
113 * @param string $class
115 public static function registerTemplateEngine($class)
117 $v = get_class_vars($class);
119 if (!empty($v['name']))
122 self::$template_engines[$name] = $class;
124 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
130 * @brief Return template engine instance.
132 * If $name is not defined, return engine defined by theme,
135 * @return ITemplateEngine Template Engine instance
137 public static function getTemplateEngine()
139 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
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];
145 $class = self::$template_engines[$template_engine];
147 self::$template_engine_instance[$template_engine] = $obj;
152 echo "template engine <tt>$template_engine</tt> is not registered!\n";
157 * @brief Returns the active template engine.
159 * @return string the active template engine
161 public static function getActiveTemplateEngine()
163 return self::$theme['template_engine'];
167 * sets the active template engine
169 * @param string $engine the template engine (default is Smarty3)
171 public static function setActiveTemplateEngine($engine = 'smarty3')
173 self::$theme['template_engine'] = $engine;
177 * Gets the right delimiter for a template engine
183 * @param string $engine The template engine (default is Smarty3)
185 * @return string the right delimiter
187 public static function getTemplateLeftDelimiter($engine = 'smarty3')
189 return self::$ldelim[$engine];
193 * Gets the left delimiter for a template engine
199 * @param string $engine The template engine (default is Smarty3)
201 * @return string the left delimiter
203 public static function getTemplateRightDelimiter($engine = 'smarty3')
205 return self::$rdelim[$engine];