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