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\Network\HTTPException\InternalServerErrorException;
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 = [
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
73 * @throws InternalServerErrorException
75 public static function replaceMacros(string $template, array $vars = [])
77 $stamp1 = microtime(true);
79 // pass $baseurl to all templates if it isn't set
80 $vars = array_merge(['$baseurl' => DI::baseUrl()->get(), '$APP' => DI::app()], $vars);
82 $t = self::getTemplateEngine();
85 $output = $t->replaceMacros($template, $vars);
86 } catch (Exception $e) {
87 DI::logger()->critical($e->getMessage(), ['template' => $template, 'vars' => $vars]);
88 $message = is_site_admin() ?
90 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
91 throw new InternalServerErrorException($message);
94 DI::profiler()->saveTimestamp($stamp1, "rendering");
100 * Load a given template $s
102 * @param string $file Template to load.
103 * @param string $subDir Subdirectory (Optional)
105 * @return string template.
106 * @throws InternalServerErrorException
108 public static function getMarkupTemplate($file, $subDir = '')
110 $stamp1 = microtime(true);
111 $t = self::getTemplateEngine();
114 $template = $t->getTemplateFile($file, $subDir);
115 } catch (Exception $e) {
116 DI::logger()->critical($e->getMessage(), ['file' => $file, 'subDir' => $subDir]);
117 $message = is_site_admin() ?
119 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
120 throw new InternalServerErrorException($message);
123 DI::profiler()->saveTimestamp($stamp1, "file");
129 * Register template engine class
131 * @param string $class
132 * @throws InternalServerErrorException
134 public static function registerTemplateEngine($class)
136 $v = get_class_vars($class);
138 if (!empty($v['name'])) {
140 self::$template_engines[$name] = $class;
142 $admin_message = DI::l10n()->t('template engine cannot be registered without a name.');
143 DI::logger()->critical($admin_message, ['class' => $class]);
144 $message = is_site_admin() ?
146 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
147 throw new InternalServerErrorException($message);
152 * Return template engine instance.
154 * If $name is not defined, return engine defined by theme,
157 * @return TemplateEngine Template Engine instance
158 * @throws InternalServerErrorException
160 public static function getTemplateEngine()
162 $template_engine = (self::$theme['template_engine'] ?? '') ?: 'smarty3';
164 if (isset(self::$template_engines[$template_engine])) {
165 if (isset(self::$template_engine_instance[$template_engine])) {
166 return self::$template_engine_instance[$template_engine];
169 $class = self::$template_engines[$template_engine];
170 $obj = new $class($a->getCurrentTheme(), $a->theme_info);
171 self::$template_engine_instance[$template_engine] = $obj;
176 $admin_message = DI::l10n()->t('template engine is not registered!');
177 DI::logger()->critical($admin_message, ['template_engine' => $template_engine]);
178 $message = is_site_admin() ?
180 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
181 throw new InternalServerErrorException($message);
185 * Returns the active template engine.
187 * @return string the active template engine
189 public static function getActiveTemplateEngine()
191 return self::$theme['template_engine'];
195 * sets the active template engine
197 * @param string $engine the template engine (default is Smarty3)
199 public static function setActiveTemplateEngine($engine = 'smarty3')
201 self::$theme['template_engine'] = $engine;
205 * Gets the right delimiter for a template engine
211 * @param string $engine The template engine (default is Smarty3)
213 * @return string the right delimiter
215 public static function getTemplateLeftDelimiter($engine = 'smarty3')
217 return self::$ldelim[$engine];
221 * Gets the left delimiter for a template engine
227 * @param string $engine The template engine (default is Smarty3)
229 * @return string the left delimiter
231 public static function getTemplateRightDelimiter($engine = 'smarty3')
233 return self::$rdelim[$engine];