]> git.mxchange.org Git - friendica.git/blob - include/friendica_smarty.php
f0d819d6232f7435647ef63af1b6ea8ad5908c25
[friendica.git] / include / friendica_smarty.php
1 <?php
2
3 use Friendica\Core\Addon;
4 use Friendica\Render\ITemplateEngine;
5
6 require_once "include/plugin.php";
7
8 define('SMARTY3_TEMPLATE_FOLDER', 'templates');
9
10 class FriendicaSmarty extends Smarty
11 {
12         public $filename;
13
14         function __construct()
15         {
16                 parent::__construct();
17
18                 $a = get_app();
19                 $theme = current_theme();
20
21                 // setTemplateDir can be set to an array, which Smarty will parse in order.
22                 // The order is thus very important here
23                 $template_dirs = ['theme' => "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"];
24                 if (x($a->theme_info, "extends"))
25                         $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"];
26                 $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"];
27                 $this->setTemplateDir($template_dirs);
28
29                 $this->setCompileDir('view/smarty3/compiled/');
30                 $this->setConfigDir('view/smarty3/config/');
31                 $this->setCacheDir('view/smarty3/cache/');
32
33                 $this->left_delimiter = $a->get_template_ldelim('smarty3');
34                 $this->right_delimiter = $a->get_template_rdelim('smarty3');
35
36                 // Don't report errors so verbosely
37                 $this->error_reporting = E_ALL & ~E_NOTICE;
38         }
39
40         function parsed($template = '')
41         {
42                 if ($template) {
43                         return $this->fetch('string:' . $template);
44                 }
45                 return $this->fetch('file:' . $this->filename);
46         }
47
48 }
49
50 class FriendicaSmartyEngine implements ITemplateEngine
51 {
52         static $name = "smarty3";
53
54         public function __construct()
55         {
56                 if (!is_writable('view/smarty3/')) {
57                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
58                         killme();
59                 }
60         }
61
62         // ITemplateEngine interface
63         public function replaceMacros($s, $r)
64         {
65                 $template = '';
66                 if (gettype($s) === 'string') {
67                         $template = $s;
68                         $s = new FriendicaSmarty();
69                 }
70
71                 $r['$APP'] = get_app();
72
73                 // "middleware": inject variables into templates
74                 $arr = [
75                         "template" => basename($s->filename),
76                         "vars" => $r
77                 ];
78                 Addon::callHooks("template_vars", $arr);
79                 $r = $arr['vars'];
80
81                 foreach ($r as $key => $value) {
82                         if ($key[0] === '$') {
83                                 $key = substr($key, 1);
84                         }
85                         $s->assign($key, $value);
86                 }
87                 return $s->parsed($template);
88         }
89
90         public function getTemplateFile($file, $root = '')
91         {
92                 $a = get_app();
93                 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root);
94                 $template = new FriendicaSmarty();
95                 $template->filename = $template_file;
96                 return $template;
97         }
98 }