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