3 use Friendica\Core\Addon;
4 use Friendica\Render\ITemplateEngine;
6 require_once "include/plugin.php";
8 define('SMARTY3_TEMPLATE_FOLDER', 'templates');
10 class FriendicaSmarty extends Smarty
14 function __construct()
16 parent::__construct();
19 $theme = current_theme();
21 // setTemplateDir can be set to an array, which Smarty will parse in order.
22 // The order is thus very important here
23 $template_dirs = ['theme' => "view/theme/$theme/" . SMARTY3_TEMPLATE_FOLDER . "/"];
24 if (x($a->theme_info, "extends"))
25 $template_dirs = $template_dirs + ['extends' => "view/theme/" . $a->theme_info["extends"] . "/" . SMARTY3_TEMPLATE_FOLDER . "/"];
26 $template_dirs = $template_dirs + ['base' => "view/" . SMARTY3_TEMPLATE_FOLDER . "/"];
27 $this->setTemplateDir($template_dirs);
29 $this->setCompileDir('view/smarty3/compiled/');
30 $this->setConfigDir('view/smarty3/config/');
31 $this->setCacheDir('view/smarty3/cache/');
33 $this->left_delimiter = $a->get_template_ldelim('smarty3');
34 $this->right_delimiter = $a->get_template_rdelim('smarty3');
36 // Don't report errors so verbosely
37 $this->error_reporting = E_ALL & ~E_NOTICE;
40 function parsed($template = '')
43 return $this->fetch('string:' . $template);
45 return $this->fetch('file:' . $this->filename);
50 class FriendicaSmartyEngine implements ITemplateEngine
52 static $name = "smarty3";
54 public function __construct()
56 if (!is_writable('view/smarty3/')) {
57 echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
62 // ITemplateEngine interface
63 public function replaceMacros($s, $r)
66 if (gettype($s) === 'string') {
68 $s = new FriendicaSmarty();
71 $r['$APP'] = get_app();
73 // "middleware": inject variables into templates
75 "template" => basename($s->filename),
78 Addon::callHooks("template_vars", $arr);
81 foreach ($r as $key => $value) {
82 if ($key[0] === '$') {
83 $key = substr($key, 1);
85 $s->assign($key, $value);
87 return $s->parsed($template);
90 public function getTemplateFile($file, $root = '')
93 $template_file = get_template_file($a, SMARTY3_TEMPLATE_FOLDER . '/' . $file, $root);
94 $template = new FriendicaSmarty();
95 $template->filename = $template_file;