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