]> git.mxchange.org Git - friendica.git/blob - include/friendica_smarty.php
Bugfix for pull request #2147 (Fix for issue #2122)
[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                 // "middleware": inject variables into templates
65                 $arr = array(
66                         "template"=> basename($s->filename),
67                         "vars" => $r
68                 );
69                 call_hooks("template_vars", $arr);
70                 $r = $arr['vars'];
71
72                 foreach($r as $key=>$value) {
73                         if($key[0] === '$') {
74                                 $key = substr($key, 1);
75                         }
76                         $s->assign($key, $value);
77                 }
78                 return $s->parsed($template);
79         }
80
81         public function get_template_file($file, $root=''){
82                 $a = get_app();
83                 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER.'/'.$file, $root);
84                 $template = new FriendicaSmarty();
85                 $template->filename = $template_file;
86                 return $template;
87         }
88 }