3 use Friendica\Render\ITemplateEngine;
5 require_once "include/plugin.php";
7 define('SMARTY3_TEMPLATE_FOLDER', 'templates');
9 class FriendicaSmarty extends Smarty
13 function __construct()
15 parent::__construct();
18 $theme = current_theme();
20 // setTemplateDir can be set to an array, which Smarty will parse in order.
21 // The order is thus very important here
22 $template_dirs = ['theme' => "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"];
23 if (x($a->theme_info, "extends"))
24 $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"];
25 $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"];
26 $this->setTemplateDir($template_dirs);
28 $this->setCompileDir('view/smarty3/compiled/');
29 $this->setConfigDir('view/smarty3/config/');
30 $this->setCacheDir('view/smarty3/cache/');
32 $this->left_delimiter = $a->get_template_ldelim('smarty3');
33 $this->right_delimiter = $a->get_template_rdelim('smarty3');
35 // Don't report errors so verbosely
36 $this->error_reporting = E_ALL & ~E_NOTICE;
39 function parsed($template = '')
42 return $this->fetch('string:' . $template);
44 return $this->fetch('file:' . $this->filename);
49 class FriendicaSmartyEngine implements ITemplateEngine
51 static $name = "smarty3";
53 public function __construct()
55 if (!is_writable('view/smarty3/')) {
56 echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
61 // ITemplateEngine interface
62 public function replaceMacros($s, $r)
65 if (gettype($s) === 'string') {
67 $s = new FriendicaSmarty();
70 $r['$APP'] = get_app();
72 // "middleware": inject variables into templates
74 "template" => basename($s->filename),
77 call_hooks("template_vars", $arr);
80 foreach ($r as $key => $value) {
81 if ($key[0] === '$') {
82 $key = substr($key, 1);
84 $s->assign($key, $value);
86 return $s->parsed($template);
89 public function getTemplateFile($file, $root = '')
92 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root);
93 $template = new FriendicaSmarty();
94 $template->filename = $template_file;