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;
28 * Smarty implementation of the Friendica template engine interface
30 class FriendicaSmartyEngine implements ITemplateEngine
32 static $name = "smarty3";
34 public function __construct()
36 if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
37 echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
42 // ITemplateEngine interface
43 public function replaceMacros($s, $r)
46 if (gettype($s) === 'string') {
48 $s = new FriendicaSmarty();
51 $r['$APP'] = DI::app();
53 // "middleware": inject variables into templates
55 "template" => basename($s->filename),
58 Hook::callAll("template_vars", $arr);
61 foreach ($r as $key => $value) {
62 if ($key[0] === '$') {
63 $key = substr($key, 1);
66 $s->assign($key, $value);
68 return $s->parsed($template);
71 public function getTemplateFile($file, $root = '')
74 $template = new FriendicaSmarty();
76 // Make sure $root ends with a slash /
77 if ($root !== '' && substr($root, -1, 1) !== '/') {
81 $theme = $a->getCurrentTheme();
82 $filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
84 if (file_exists("{$root}view/theme/$theme/$filename")) {
85 $template_file = "{$root}view/theme/$theme/$filename";
86 } elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
87 $template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
88 } elseif (file_exists("{$root}/$filename")) {
89 $template_file = "{$root}/$filename";
91 $template_file = "{$root}view/$filename";
94 $template->filename = $template_file;