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\Render;
24 use Friendica\Core\Hook;
26 use Friendica\Network\HTTPException\ServiceUnavailableException;
27 use Friendica\Util\Strings;
30 * Smarty implementation of the Friendica template abstraction
32 final class FriendicaSmartyEngine extends TemplateEngine
34 static $name = "smarty3";
36 const FILE_PREFIX = 'file:';
37 const STRING_PREFIX = 'string:';
39 /** @var FriendicaSmarty */
45 public function __construct(string $theme, array $theme_info)
47 $this->theme = $theme;
48 $this->theme_info = $theme_info;
49 $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
51 if (!is_writable(DI::basePath() . '/view/smarty3')) {
52 $admin_message = DI::l10n()->t('The folder view/smarty3/ must be writable by webserver.');
53 DI::logger()->critical($admin_message);
54 $message = DI::app()->isSiteAdmin() ?
56 DI::l10n()->t('Friendica can\'t display this page at the moment, please contact the administrator.');
57 throw new ServiceUnavailableException($message);
64 public function testInstall(array &$errors = null)
66 $this->smarty->testInstall($errors);
72 public function replaceMacros(string $template, array $vars)
74 if (!Strings::startsWith($template, self::FILE_PREFIX)) {
75 $template = self::STRING_PREFIX . $template;
78 // "middleware": inject variables into templates
80 'template' => basename($this->smarty->filename),
83 Hook::callAll('template_vars', $arr);
86 $this->smarty->clearAllAssign();
88 foreach ($vars as $key => $value) {
89 if ($key[0] === '$') {
90 $key = substr($key, 1);
93 $this->smarty->assign($key, $value);
96 return $this->smarty->fetch($template);
102 public function getTemplateFile(string $file, string $subDir = '')
104 // Make sure $root ends with a slash /
105 if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
106 $subDir = $subDir . '/';
109 $root = DI::basePath() . '/' . $subDir;
111 $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
113 if (file_exists("{$root}view/theme/$this->theme/$filename")) {
114 $template_file = "{$root}view/theme/$this->theme/$filename";
115 } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
116 $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
117 } elseif (file_exists("{$root}/$filename")) {
118 $template_file = "{$root}/$filename";
120 $template_file = "{$root}view/$filename";
123 $this->smarty->filename = $template_file;
125 return self::FILE_PREFIX . $template_file;