3 use Friendica\Core\Addon;
4 use Friendica\Render\ITemplateEngine;
6 define('SMARTY3_TEMPLATE_FOLDER', 'templates');
8 class FriendicaSmarty extends Smarty
12 function __construct()
14 parent::__construct();
17 $theme = current_theme();
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 = ['theme' => "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"];
22 if (x($a->theme_info, "extends"))
23 $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"];
24 $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"];
25 $this->setTemplateDir($template_dirs);
27 $this->setCompileDir('view/smarty3/compiled/');
28 $this->setConfigDir('view/smarty3/config/');
29 $this->setCacheDir('view/smarty3/cache/');
31 $this->left_delimiter = $a->get_template_ldelim('smarty3');
32 $this->right_delimiter = $a->get_template_rdelim('smarty3');
34 // Don't report errors so verbosely
35 $this->error_reporting = E_ALL & ~E_NOTICE;
38 function parsed($template = '')
41 return $this->fetch('string:' . $template);
43 return $this->fetch('file:' . $this->filename);
48 class FriendicaSmartyEngine implements ITemplateEngine
50 static $name = "smarty3";
52 public function __construct()
54 if (!is_writable('view/smarty3/')) {
55 echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
60 // ITemplateEngine interface
61 public function replaceMacros($s, $r)
64 if (gettype($s) === 'string') {
66 $s = new FriendicaSmarty();
69 $r['$APP'] = get_app();
71 // "middleware": inject variables into templates
73 "template" => basename($s->filename),
76 Addon::callHooks("template_vars", $arr);
79 foreach ($r as $key => $value) {
80 if ($key[0] === '$') {
81 $key = substr($key, 1);
83 $s->assign($key, $value);
85 return $s->parsed($template);
88 public function getTemplateFile($file, $root = '')
91 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root);
92 $template = new FriendicaSmarty();
93 $template->filename = $template_file;