3 * @file src/Core/Renderer.php
6 namespace Friendica\Core;
10 use Friendica\Render\FriendicaSmarty;
11 use Friendica\Render\ITemplateEngine;
14 * This class handles Renderer related functions.
19 * An array of registered template engines ('name'=>'class name')
21 public static $template_engines = [];
24 * An array of instanced template engines ('name'=>'instance')
26 public static $template_engine_instance = [];
29 * 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 * 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);
65 // pass $baseurl to all templates if it isn't set
66 $vars = array_merge(['$baseurl' => DI::baseUrl()->get()], $vars);
68 $t = self::getTemplateEngine();
71 $output = $t->replaceMacros($s, $vars);
72 } catch (Exception $e) {
73 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
77 DI::profiler()->saveTimestamp($stamp1, "rendering", System::callstack());
83 * Load a given template $s
85 * @param string $s Template to load.
86 * @param string $root Optional.
88 * @return string template.
91 public static function getMarkupTemplate($s, $root = '')
93 $stamp1 = microtime(true);
95 $t = self::getTemplateEngine();
98 $template = $t->getTemplateFile($s, $root);
99 } catch (Exception $e) {
100 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
104 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
110 * Register template engine class
112 * @param string $class
114 public static function registerTemplateEngine($class)
116 $v = get_class_vars($class);
118 if (!empty($v['name']))
121 self::$template_engines[$name] = $class;
123 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
129 * Return template engine instance.
131 * If $name is not defined, return engine defined by theme,
134 * @return ITemplateEngine Template Engine instance
136 public static function getTemplateEngine()
138 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
140 if (isset(self::$template_engines[$template_engine])) {
141 if (isset(self::$template_engine_instance[$template_engine])) {
142 return self::$template_engine_instance[$template_engine];
144 $class = self::$template_engines[$template_engine];
146 self::$template_engine_instance[$template_engine] = $obj;
151 echo "template engine <tt>$template_engine</tt> is not registered!\n";
156 * Returns the active template engine.
158 * @return string the active template engine
160 public static function getActiveTemplateEngine()
162 return self::$theme['template_engine'];
166 * sets the active template engine
168 * @param string $engine the template engine (default is Smarty3)
170 public static function setActiveTemplateEngine($engine = 'smarty3')
172 self::$theme['template_engine'] = $engine;
176 * Gets the right delimiter for a template engine
182 * @param string $engine The template engine (default is Smarty3)
184 * @return string the right delimiter
186 public static function getTemplateLeftDelimiter($engine = 'smarty3')
188 return self::$ldelim[$engine];
192 * Gets the left delimiter for a template engine
198 * @param string $engine The template engine (default is Smarty3)
200 * @return string the left delimiter
202 public static function getTemplateRightDelimiter($engine = 'smarty3')
204 return self::$rdelim[$engine];