3 * @file src/Core/Renderer.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
11 use Friendica\Render\FriendicaSmarty;
12 use Friendica\Render\ITemplateEngine;
15 * @brief This class handles Renderer related functions.
17 class Renderer extends BaseObject
20 * @brief An array of registered template engines ('name'=>'class name')
22 public static $template_engines = [];
25 * @brief An array of instanced template engines ('name'=>'instance')
27 public static $template_engine_instance = [];
30 * @brief An array for all theme-controllable parameters
32 * Mostly unimplemented yet. Only options 'template_engine' and
35 public static $theme = [
39 'force_max_items' => 0,
41 'template_engine' => 'smarty3',
44 private static $ldelim = [
48 private static $rdelim = [
54 * @brief This is our template processor
56 * @param string|FriendicaSmarty $s The string requiring macro substitution or an instance of FriendicaSmarty
57 * @param array $vars Key value pairs (search => replace)
59 * @return string substituted string
62 public static function replaceMacros($s, array $vars = [])
64 $stamp1 = microtime(true);
67 // pass $baseurl to all templates if it isn't set
68 $vars = array_merge(['$baseurl' => $a->getBaseURL()], $vars);
70 $t = self::getTemplateEngine();
73 $output = $t->replaceMacros($s, $vars);
74 } catch (Exception $e) {
75 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
79 $a->getProfiler()->saveTimestamp($stamp1, "rendering", System::callstack());
85 * @brief Load a given template $s
87 * @param string $s Template to load.
88 * @param string $root Optional.
90 * @return string template.
93 public static function getMarkupTemplate($s, $root = '')
95 $stamp1 = microtime(true);
97 $t = self::getTemplateEngine();
100 $template = $t->getTemplateFile($s, $root);
101 } catch (Exception $e) {
102 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
106 $a->getProfiler()->saveTimestamp($stamp1, "file", System::callstack());
112 * @brief Register template engine class
114 * @param string $class
116 public static function registerTemplateEngine($class)
118 $v = get_class_vars($class);
120 if (!empty($v['name']))
123 self::$template_engines[$name] = $class;
125 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
131 * @brief Return template engine instance.
133 * If $name is not defined, return engine defined by theme,
136 * @return ITemplateEngine Template Engine instance
138 public static function getTemplateEngine()
140 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
142 if (isset(self::$template_engines[$template_engine])) {
143 if (isset(self::$template_engine_instance[$template_engine])) {
144 return self::$template_engine_instance[$template_engine];
146 $class = self::$template_engines[$template_engine];
148 self::$template_engine_instance[$template_engine] = $obj;
153 echo "template engine <tt>$template_engine</tt> is not registered!\n";
158 * @brief Returns the active template engine.
160 * @return string the active template engine
162 public static function getActiveTemplateEngine()
164 return self::$theme['template_engine'];
168 * sets the active template engine
170 * @param string $engine the template engine (default is Smarty3)
172 public static function setActiveTemplateEngine($engine = 'smarty3')
174 self::$theme['template_engine'] = $engine;
178 * Gets the right delimiter for a template engine
184 * @param string $engine The template engine (default is Smarty3)
186 * @return string the right delimiter
188 public static function getTemplateLeftDelimiter($engine = 'smarty3')
190 return self::$ldelim[$engine];
194 * Gets the left delimiter for a template engine
200 * @param string $engine The template engine (default is Smarty3)
202 * @return string the left delimiter
204 public static function getTemplateRightDelimiter($engine = 'smarty3')
206 return self::$rdelim[$engine];