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