]> git.mxchange.org Git - friendica.git/blob - include/friendica_smarty.php
Split the name in the hcard
[friendica.git] / include / friendica_smarty.php
1 <?php
2
3 use Friendica\Render\ITemplateEngine;
4
5 require_once("library/Smarty/libs/Smarty.class.php");
6 require_once "include/plugin.php";
7
8 define('SMARTY3_TEMPLATE_FOLDER','templates');
9
10 class FriendicaSmarty extends Smarty {
11         public $filename;
12
13         function __construct() {
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 = array('theme' => "view/theme/$theme/".SMARTY3_TEMPLATE_FOLDER."/");
22                 if ( x($a->theme_info,"extends") )
23                         $template_dirs = $template_dirs + array('extends' => "view/theme/".$a->theme_info["extends"]."/".SMARTY3_TEMPLATE_FOLDER."/");
24                 $template_dirs = $template_dirs + array('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                 if ($template) {
40                         return $this->fetch('string:' . $template);
41                 }
42                 return $this->fetch('file:' . $this->filename);
43         }
44
45
46 }
47
48 class FriendicaSmartyEngine implements ITemplateEngine {
49         static $name ="smarty3";
50
51     public function __construct(){
52                 if (!is_writable('view/smarty3/')){
53                         echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver."; killme();
54                 }
55         }
56
57         // ITemplateEngine interface
58         public function replaceMacros($s, $r) {
59                 $template = '';
60                 if (gettype($s) === 'string') {
61                         $template = $s;
62                         $s = new FriendicaSmarty();
63                 }
64
65                 $r['$APP'] = get_app();
66
67                 // "middleware": inject variables into templates
68                 $arr = array(
69                         "template"=> basename($s->filename),
70                         "vars" => $r
71                 );
72                 call_hooks("template_vars", $arr);
73                 $r = $arr['vars'];
74
75                 foreach ($r as $key=>$value) {
76                         if ($key[0] === '$') {
77                                 $key = substr($key, 1);
78                         }
79                         $s->assign($key, $value);
80                 }
81                 return $s->parsed($template);
82         }
83
84         public function getTemplateFile($file, $root=''){
85                 $a = get_app();
86                 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER.'/'.$file, $root);
87                 $template = new FriendicaSmarty();
88                 $template->filename = $template_file;
89                 return $template;
90         }
91 }