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\Render;
24 use Friendica\Core\Hook;
26 use Friendica\Util\Strings;
29 * Smarty implementation of the Friendica template abstraction
31 final class FriendicaSmartyEngine extends TemplateEngine
33 static $name = "smarty3";
35 const FILE_PREFIX = 'file:';
36 const STRING_PREFIX = 'string:';
38 /** @var FriendicaSmarty */
44 public function __construct(string $theme, array $theme_info)
46 $this->theme = $theme;
47 $this->theme_info = $theme_info;
48 $this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
50 if (!is_writable(DI::basePath() . '/view/smarty3')) {
51 echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
59 public function testInstall(array &$errors = null)
61 $this->smarty->testInstall($errors);
67 public function replaceMacros(string $template, array $vars)
69 if (!Strings::startsWith($template, self::FILE_PREFIX)) {
70 $template = self::STRING_PREFIX . $template;
73 // "middleware": inject variables into templates
75 'template' => basename($this->smarty->filename),
78 Hook::callAll('template_vars', $arr);
81 foreach ($vars as $key => $value) {
82 if ($key[0] === '$') {
83 $key = substr($key, 1);
86 $this->smarty->assign($key, $value);
89 return $this->smarty->fetch($template);
95 public function getTemplateFile(string $file, string $subDir = '')
97 // Make sure $root ends with a slash /
98 if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
99 $subDir = $subDir . '/';
102 $root = DI::basePath() . '/' . $subDir;
104 $filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
106 if (file_exists("{$root}view/theme/$this->theme/$filename")) {
107 $template_file = "{$root}view/theme/$this->theme/$filename";
108 } elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
109 $template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
110 } elseif (file_exists("{$root}/$filename")) {
111 $template_file = "{$root}/$filename";
113 $template_file = "{$root}view/$filename";
116 $this->smarty->filename = $template_file;
118 return self::FILE_PREFIX . $template_file;