2018-02-03 10:11:00 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Render/FriendicaSmartyEngine.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Render;
|
|
|
|
|
2018-12-26 06:06:24 +00:00
|
|
|
use Friendica\Core\Hook;
|
2018-02-03 10:11:00 +00:00
|
|
|
|
2018-02-03 13:52:43 +00:00
|
|
|
/**
|
|
|
|
* Smarty implementation of the Friendica template engine interface
|
|
|
|
*
|
2018-09-15 23:28:38 +00:00
|
|
|
* @author Hypolite Petovan <hypolite@mrpetovan.com>
|
2018-02-03 13:52:43 +00:00
|
|
|
*/
|
2018-02-03 10:11:00 +00:00
|
|
|
class FriendicaSmartyEngine implements ITemplateEngine
|
|
|
|
{
|
|
|
|
static $name = "smarty3";
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-03-10 04:21:19 +00:00
|
|
|
if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
|
2018-02-03 10:11:00 +00:00
|
|
|
echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
|
2018-12-26 05:40:12 +00:00
|
|
|
exit();
|
2018-02-03 10:11:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ITemplateEngine interface
|
|
|
|
public function replaceMacros($s, $r)
|
|
|
|
{
|
|
|
|
$template = '';
|
|
|
|
if (gettype($s) === 'string') {
|
|
|
|
$template = $s;
|
|
|
|
$s = new FriendicaSmarty();
|
|
|
|
}
|
|
|
|
|
2018-12-28 00:22:35 +00:00
|
|
|
$r['$APP'] = \get_app();
|
2018-02-03 10:11:00 +00:00
|
|
|
|
|
|
|
// "middleware": inject variables into templates
|
|
|
|
$arr = [
|
|
|
|
"template" => basename($s->filename),
|
|
|
|
"vars" => $r
|
|
|
|
];
|
2018-12-26 06:06:24 +00:00
|
|
|
Hook::callAll("template_vars", $arr);
|
2018-02-03 10:11:00 +00:00
|
|
|
$r = $arr['vars'];
|
|
|
|
|
|
|
|
foreach ($r as $key => $value) {
|
|
|
|
if ($key[0] === '$') {
|
|
|
|
$key = substr($key, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$s->assign($key, $value);
|
|
|
|
}
|
|
|
|
return $s->parsed($template);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTemplateFile($file, $root = '')
|
|
|
|
{
|
2018-12-28 00:22:35 +00:00
|
|
|
$a = \get_app();
|
2018-02-03 10:11:00 +00:00
|
|
|
$template = new FriendicaSmarty();
|
2018-02-03 13:52:43 +00:00
|
|
|
|
|
|
|
// Make sure $root ends with a slash /
|
|
|
|
if ($root !== '' && substr($root, -1, 1) !== '/') {
|
|
|
|
$root = $root . '/';
|
|
|
|
}
|
|
|
|
|
2018-04-28 22:37:04 +00:00
|
|
|
$theme = $a->getCurrentTheme();
|
2018-02-03 13:52:43 +00:00
|
|
|
$filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
|
|
|
|
|
|
|
|
if (file_exists("{$root}view/theme/$theme/$filename")) {
|
|
|
|
$template_file = "{$root}view/theme/$theme/$filename";
|
2018-11-30 14:06:22 +00:00
|
|
|
} elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
|
2018-02-03 13:52:43 +00:00
|
|
|
$template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
|
|
|
|
} elseif (file_exists("{$root}/$filename")) {
|
|
|
|
$template_file = "{$root}/$filename";
|
|
|
|
} else {
|
|
|
|
$template_file = "{$root}view/$filename";
|
|
|
|
}
|
|
|
|
|
2018-02-03 10:11:00 +00:00
|
|
|
$template->filename = $template_file;
|
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
}
|