]> git.mxchange.org Git - friendica.git/blob - src/Render/FriendicaSmartyEngine.php
1053ab08e064b2121191fee4a39e327f69612334
[friendica.git] / src / Render / FriendicaSmartyEngine.php
1 <?php
2 /**
3  * @file src/Render/FriendicaSmartyEngine.php
4  */
5 namespace Friendica\Render;
6
7 use Friendica\Core\Addon;
8
9 /**
10  * Smarty implementation of the Friendica template engine interface
11  *
12  * @author Hypolite Petovan <hypolite@mrpetovan.com>
13  */
14 class FriendicaSmartyEngine implements ITemplateEngine
15 {
16         static $name = "smarty3";
17
18         public function __construct()
19         {
20                 if (!is_writable('view/smarty3/')) {
21                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
22                         killme();
23                 }
24         }
25
26         // ITemplateEngine interface
27         public function replaceMacros($s, $r)
28         {
29                 $template = '';
30                 if (gettype($s) === 'string') {
31                         $template = $s;
32                         $s = new FriendicaSmarty();
33                 }
34
35                 $r['$APP'] = get_app();
36
37                 // "middleware": inject variables into templates
38                 $arr = [
39                         "template" => basename($s->filename),
40                         "vars" => $r
41                 ];
42                 Addon::callHooks("template_vars", $arr);
43                 $r = $arr['vars'];
44
45                 foreach ($r as $key => $value) {
46                         if ($key[0] === '$') {
47                                 $key = substr($key, 1);
48                         }
49
50                         $s->assign($key, $value);
51                 }
52                 return $s->parsed($template);
53         }
54
55         public function getTemplateFile($file, $root = '')
56         {
57                 $a = get_app();
58                 $template = new FriendicaSmarty();
59
60                 // Make sure $root ends with a slash /
61                 if ($root !== '' && substr($root, -1, 1) !== '/') {
62                         $root = $root . '/';
63                 }
64
65                 $theme = $a->getCurrentTheme();
66                 $filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
67
68                 if (file_exists("{$root}view/theme/$theme/$filename")) {
69                         $template_file = "{$root}view/theme/$theme/$filename";
70                 } elseif (x($a->theme_info, 'extends') && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
71                         $template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
72                 } elseif (file_exists("{$root}/$filename")) {
73                         $template_file = "{$root}/$filename";
74                 } else {
75                         $template_file = "{$root}view/$filename";
76                 }
77
78                 $template->filename = $template_file;
79
80                 return $template;
81         }
82 }