3 * @file src/Core/Renderer.php
6 namespace Friendica\Core;
9 use Friendica\BaseObject;
10 use Friendica\Core\System;
11 use Friendica\Render\FriendicaSmarty;
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 $r key value pairs (search => replace)
58 * @return string substituted string
60 public static function replaceMacros($s, $r)
62 $stamp1 = microtime(true);
65 // pass $baseurl to all templates
66 $r['$baseurl'] = System::baseUrl();
67 $t = self::getTemplateEngine();
70 $output = $t->replaceMacros($s, $r);
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.
89 public static function getMarkupTemplate($s, $root = '')
91 $stamp1 = microtime(true);
93 $t = self::getTemplateEngine();
96 $template = $t->getTemplateFile($s, $root);
97 } catch (Exception $e) {
98 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
102 $a->saveTimestamp($stamp1, "file");
108 * @brief Register template engine class
110 * @param string $class
112 public static function registerTemplateEngine($class)
114 $v = get_class_vars($class);
116 if (!empty($v['name']))
119 self::$template_engines[$name] = $class;
121 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
127 * @brief Return template engine instance.
129 * If $name is not defined, return engine defined by theme,
132 * @return object Template Engine instance
134 public static function getTemplateEngine()
136 $template_engine = defaults(self::$theme, 'template_engine', 'smarty3');
138 if (isset(self::$template_engines[$template_engine])) {
139 if (isset(self::$template_engine_instance[$template_engine])) {
140 return self::$template_engine_instance[$template_engine];
142 $class = self::$template_engines[$template_engine];
144 self::$template_engine_instance[$template_engine] = $obj;
149 echo "template engine <tt>$template_engine</tt> is not registered!\n";
154 * @brief Returns the active template engine.
156 * @return string the active template engine
158 public static function getActiveTemplateEngine()
160 return self::$theme['template_engine'];
164 * sets the active template engine
166 * @param string $engine the template engine (default is Smarty3)
168 public static function setActiveTemplateEngine($engine = 'smarty3')
170 self::$theme['template_engine'] = $engine;
174 * Gets the right delimiter for a template engine
180 * @param string $engine The template engine (default is Smarty3)
182 * @return string the right delimiter
184 public static function getTemplateLeftDelimiter($engine = 'smarty3')
186 return self::$ldelim[$engine];
190 * Gets the left delimiter for a template engine
196 * @param string $engine The template engine (default is Smarty3)
198 * @return string the left delimiter
200 public static function getTemplateRightDelimiter($engine = 'smarty3')
202 return self::$rdelim[$engine];