]> git.mxchange.org Git - friendica.git/blob - include/friendica_smarty.php
Merge remote branch 'upstream/master'
[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
6 define('SMARTY3_TEMPLATE_FOLDER','templates');
7
8 class FriendicaSmarty extends Smarty {
9         public $filename;
10
11         function __construct() {
12                 parent::__construct();
13
14                 $a = get_app();
15                 $theme = current_theme();
16
17                 // setTemplateDir can be set to an array, which Smarty will parse in order.
18                 // The order is thus very important here
19                 $template_dirs = array('theme' => "view/theme/$theme/".SMARTY3_TEMPLATE_FOLDER."/");
20                 if( x($a->theme_info,"extends") )
21                         $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/".SMARTY3_TEMPLATE_FOLDER."/");
22                 $template_dirs = $template_dirs + array('base' => "view/".SMARTY3_TEMPLATE_FOLDER."/");
23                 $this->setTemplateDir($template_dirs);
24
25                 $this->setCompileDir('view/smarty3/compiled/');
26                 $this->setConfigDir('view/smarty3/config/');
27                 $this->setCacheDir('view/smarty3/cache/');
28
29                 $this->left_delimiter = $a->get_template_ldelim('smarty3');
30                 $this->right_delimiter = $a->get_template_rdelim('smarty3');
31
32                 // Don't report errors so verbosely
33                 $this->error_reporting = E_ALL & ~E_NOTICE;
34         }
35
36         function parsed($template = '') {
37                 if($template) {
38                         return $this->fetch('string:' . $template);
39                 }
40                 return $this->fetch('file:' . $this->filename);
41         }
42         
43
44 }
45
46 class FriendicaSmartyEngine implements ITemplateEngine {
47         static $name ="smarty3";
48         
49     public function __construct(){
50                 if(!is_writable('view/smarty3/')){
51                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver."; killme();
52                 }
53         } 
54     
55         // ITemplateEngine interface
56         public function replace_macros($s, $r) {
57                 $template = '';
58                 if(gettype($s) === 'string') {
59                         $template = $s;
60                         $s = new FriendicaSmarty();
61                 }
62                 foreach($r as $key=>$value) {
63                         if($key[0] === '$') {
64                                 $key = substr($key, 1);
65                         }
66                         $s->assign($key, $value);
67                 }
68                 return $s->parsed($template);           
69         }
70         
71         public function get_template_file($file, $root=''){
72                 $a = get_app();
73                 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER.'/'.$file, $root);
74                 $template = new FriendicaSmarty();
75                 $template->filename = $template_file;
76                 return $template;
77         }
78 }