3 * @copyright Copyright (C) 2010-2022, the Friendica project
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\Network\HTTPException\ServiceUnavailableException;
27 use Friendica\Render\TemplateEngine;
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 'template_engine' => 'smarty3',
57 private static $ldelim = [
61 private static $rdelim = [
67 * Returns the rendered template output from the template string and variables
69 * @param string $template
72 * @throws ServiceUnavailableException
74 public static function replaceMacros(string $template, array $vars = []): string
76 DI::profiler()->startRecording('rendering');
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 DI::logger()->critical($e->getMessage(), ['template' => $template, 'vars' => $vars]);
87 $message = DI::app()->isSiteAdmin() ?
89 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
90 throw new ServiceUnavailableException($message);
93 DI::profiler()->stopRecording();
99 * Load a given template $s
101 * @param string $file Template to load.
102 * @param string $subDir Subdirectory (Optional)
104 * @return string Template
105 * @throws ServiceUnavailableException
107 public static function getMarkupTemplate(string $file, string $subDir = ''): string
109 DI::profiler()->startRecording('file');
110 $t = self::getTemplateEngine();
113 $template = $t->getTemplateFile($file, $subDir);
114 } catch (Exception $e) {
115 DI::logger()->critical($e->getMessage(), ['file' => $file, 'subDir' => $subDir]);
116 $message = DI::app()->isSiteAdmin() ?
118 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
119 throw new ServiceUnavailableException($message);
122 DI::profiler()->stopRecording();
128 * Register template engine class
130 * @param string $class
133 * @throws ServiceUnavailableException
135 public static function registerTemplateEngine(string $class)
137 $v = get_class_vars($class);
139 if (!empty($v['name'])) {
141 self::$template_engines[$name] = $class;
143 $admin_message = DI::l10n()->t('template engine cannot be registered without a name.');
144 DI::logger()->critical($admin_message, ['class' => $class]);
145 $message = DI::app()->isSiteAdmin() ?
147 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
148 throw new ServiceUnavailableException($message);
153 * Return template engine instance.
155 * If $name is not defined, return engine defined by theme,
158 * @return TemplateEngine Template Engine instance
159 * @throws ServiceUnavailableException
161 public static function getTemplateEngine(): TemplateEngine
163 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
165 if (isset(self::$template_engines[$template_engine])) {
166 if (isset(self::$template_engine_instance[$template_engine])) {
167 return self::$template_engine_instance[$template_engine];
170 $class = self::$template_engines[$template_engine];
171 $obj = new $class($a->getCurrentTheme(), $a->getThemeInfo());
172 self::$template_engine_instance[$template_engine] = $obj;
177 $admin_message = DI::l10n()->t('template engine is not registered!');
178 DI::logger()->critical($admin_message, ['template_engine' => $template_engine]);
179 $message = DI::app()->isSiteAdmin() ?
181 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
182 throw new ServiceUnavailableException($message);
186 * Returns the active template engine.
188 * @return string the active template engine
190 public static function getActiveTemplateEngine(): string
192 return self::$theme['template_engine'];
196 * sets the active template engine
198 * @param string $engine the template engine (default is Smarty3)
202 public static function setActiveTemplateEngine(string $engine = 'smarty3')
204 self::$theme['template_engine'] = $engine;
208 * Gets the right delimiter for a template engine
214 * @param string $engine The template engine (default is Smarty3)
216 * @return string the right delimiter
218 public static function getTemplateLeftDelimiter(string $engine = 'smarty3'): string
220 return self::$ldelim[$engine];
224 * Gets the left delimiter for a template engine
230 * @param string $engine The template engine (default is Smarty3)
232 * @return string the left delimiter
234 public static function getTemplateRightDelimiter(string $engine = 'smarty3'): string
236 return self::$rdelim[$engine];