3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core;
26 use Friendica\Render\FriendicaSmarty;
27 use Friendica\Render\ITemplateEngine;
30 * This class handles Renderer related functions.
35 * An array of registered template engines ('name'=>'class name')
37 public static $template_engines = [];
40 * An array of instanced template engines ('name'=>'instance')
42 public static $template_engine_instance = [];
45 * An array for all theme-controllable parameters
47 * Mostly unimplemented yet. Only options 'template_engine' and
50 public static $theme = [
54 'force_max_items' => 0,
56 'template_engine' => 'smarty3',
59 private static $ldelim = [
63 private static $rdelim = [
69 * This is our template processor
71 * @param string|FriendicaSmarty $s The string requiring macro substitution or an instance of FriendicaSmarty
72 * @param array $vars Key value pairs (search => replace)
74 * @return string substituted string
77 public static function replaceMacros($s, array $vars = [])
79 $stamp1 = microtime(true);
81 // pass $baseurl to all templates if it isn't set
82 $vars = array_merge(['$baseurl' => DI::baseUrl()->get()], $vars);
84 $t = self::getTemplateEngine();
87 $output = $t->replaceMacros($s, $vars);
88 } catch (Exception $e) {
89 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
93 DI::profiler()->saveTimestamp($stamp1, "rendering", System::callstack());
99 * Load a given template $s
101 * @param string $s Template to load.
102 * @param string $root Optional.
104 * @return string template.
107 public static function getMarkupTemplate($s, $root = '')
109 $stamp1 = microtime(true);
111 $t = self::getTemplateEngine();
114 $template = $t->getTemplateFile($s, $root);
115 } catch (Exception $e) {
116 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
120 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
126 * Register template engine class
128 * @param string $class
130 public static function registerTemplateEngine($class)
132 $v = get_class_vars($class);
134 if (!empty($v['name']))
137 self::$template_engines[$name] = $class;
139 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
145 * Return template engine instance.
147 * If $name is not defined, return engine defined by theme,
150 * @return ITemplateEngine Template Engine instance
152 public static function getTemplateEngine()
154 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
156 if (isset(self::$template_engines[$template_engine])) {
157 if (isset(self::$template_engine_instance[$template_engine])) {
158 return self::$template_engine_instance[$template_engine];
160 $class = self::$template_engines[$template_engine];
162 self::$template_engine_instance[$template_engine] = $obj;
167 echo "template engine <tt>$template_engine</tt> is not registered!\n";
172 * Returns the active template engine.
174 * @return string the active template engine
176 public static function getActiveTemplateEngine()
178 return self::$theme['template_engine'];
182 * sets the active template engine
184 * @param string $engine the template engine (default is Smarty3)
186 public static function setActiveTemplateEngine($engine = 'smarty3')
188 self::$theme['template_engine'] = $engine;
192 * Gets the right delimiter for a template engine
198 * @param string $engine The template engine (default is Smarty3)
200 * @return string the right delimiter
202 public static function getTemplateLeftDelimiter($engine = 'smarty3')
204 return self::$ldelim[$engine];
208 * Gets the left delimiter for a template engine
214 * @param string $engine The template engine (default is Smarty3)
216 * @return string the left delimiter
218 public static function getTemplateRightDelimiter($engine = 'smarty3')
220 return self::$rdelim[$engine];