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\TemplateEngine;
29 * This class handles Renderer related functions.
34 * An array of registered template engines ('name'=>'class name')
36 public static $template_engines = [];
39 * An array of instanced template engines ('name'=>'instance')
41 public static $template_engine_instance = [];
44 * An array for all theme-controllable parameters
46 * Mostly unimplemented yet. Only options 'template_engine' and
49 public static $theme = [
53 'force_max_items' => 0,
55 'template_engine' => 'smarty3',
58 private static $ldelim = [
62 private static $rdelim = [
68 * Returns the rendered template output from the template string and variables
70 * @param string $template
74 public static function replaceMacros(string $template, array $vars)
76 $stamp1 = microtime(true);
78 // pass $baseurl to all templates if it isn't set
79 $vars = array_merge(['$baseurl' => DI::baseUrl()->get(), '$APP' => DI::app()], $vars);
81 $t = self::getTemplateEngine();
84 $output = $t->replaceMacros($template, $vars);
85 } catch (Exception $e) {
86 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
90 DI::profiler()->saveTimestamp($stamp1, "rendering", System::callstack());
96 * Load a given template $s
98 * @param string $file Template to load.
99 * @param string $subDir Subdirectory (Optional)
101 * @return string template.
104 public static function getMarkupTemplate($file, $subDir = '')
106 $stamp1 = microtime(true);
107 $t = self::getTemplateEngine();
110 $template = $t->getTemplateFile($file, $subDir);
111 } catch (Exception $e) {
112 echo "<pre><b>" . __FUNCTION__ . "</b>: " . $e->getMessage() . "</pre>";
116 DI::profiler()->saveTimestamp($stamp1, "file", System::callstack());
122 * Register template engine class
124 * @param string $class
126 public static function registerTemplateEngine($class)
128 $v = get_class_vars($class);
130 if (!empty($v['name'])) {
132 self::$template_engines[$name] = $class;
134 echo "template engine <tt>$class</tt> cannot be registered without a name.\n";
140 * Return template engine instance.
142 * If $name is not defined, return engine defined by theme,
145 * @return TemplateEngine Template Engine instance
147 public static function getTemplateEngine()
149 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
151 if (isset(self::$template_engines[$template_engine])) {
152 if (isset(self::$template_engine_instance[$template_engine])) {
153 return self::$template_engine_instance[$template_engine];
156 $class = self::$template_engines[$template_engine];
157 $obj = new $class($a->getCurrentTheme(), $a->theme_info);
158 self::$template_engine_instance[$template_engine] = $obj;
163 echo "template engine <tt>$template_engine</tt> is not registered!\n";
168 * Returns the active template engine.
170 * @return string the active template engine
172 public static function getActiveTemplateEngine()
174 return self::$theme['template_engine'];
178 * sets the active template engine
180 * @param string $engine the template engine (default is Smarty3)
182 public static function setActiveTemplateEngine($engine = 'smarty3')
184 self::$theme['template_engine'] = $engine;
188 * Gets the right delimiter for a template engine
194 * @param string $engine The template engine (default is Smarty3)
196 * @return string the right delimiter
198 public static function getTemplateLeftDelimiter($engine = 'smarty3')
200 return self::$ldelim[$engine];
204 * Gets the left delimiter for a template engine
210 * @param string $engine The template engine (default is Smarty3)
212 * @return string the left delimiter
214 public static function getTemplateRightDelimiter($engine = 'smarty3')
216 return self::$rdelim[$engine];