mirror of
https://github.com/friendica/friendica
synced 2025-04-27 18:30:12 +00:00
Refactor template engine code
- Convert ITemplateEngine interface to abstract class - Remove dependency to App in FriendicaSmarty and FriendicaSmartyEngine - Normalize replaceMacro parameter behavior using Smarty template string prefixes - Remove obsolete FriendicaSmarty->parsed method - Update unused Smarty directory paths
This commit is contained in:
parent
75a0b80888
commit
c6ba92c43d
5 changed files with 97 additions and 72 deletions
|
@ -23,56 +23,69 @@ namespace Friendica\Render;
|
|||
|
||||
use Friendica\Core\Hook;
|
||||
use Friendica\DI;
|
||||
use Friendica\Util\Strings;
|
||||
|
||||
/**
|
||||
* Smarty implementation of the Friendica template engine interface
|
||||
* Smarty implementation of the Friendica template abstraction
|
||||
*/
|
||||
class FriendicaSmartyEngine implements ITemplateEngine
|
||||
final class FriendicaSmartyEngine extends TemplateEngine
|
||||
{
|
||||
static $name = "smarty3";
|
||||
|
||||
public function __construct()
|
||||
const FILE_PREFIX = 'file:';
|
||||
const STRING_PREFIX = 'string:';
|
||||
|
||||
/** @var FriendicaSmarty */
|
||||
private $smarty;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function __construct(string $theme, array $theme_info)
|
||||
{
|
||||
if (!is_writable(__DIR__ . '/../../view/smarty3/')) {
|
||||
$this->theme = $theme;
|
||||
$this->theme_info = $theme_info;
|
||||
$this->smarty = new FriendicaSmarty($this->theme, $this->theme_info);
|
||||
|
||||
if (!is_writable(DI::basePath() . '/view/smarty3')) {
|
||||
echo "<b>ERROR:</b> folder <tt>view/smarty3/</tt> must be writable by webserver.";
|
||||
exit();
|
||||
}
|
||||
}
|
||||
|
||||
// ITemplateEngine interface
|
||||
public function replaceMacros($s, $r)
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function replaceMacros(string $template, array $vars)
|
||||
{
|
||||
$template = '';
|
||||
if (gettype($s) === 'string') {
|
||||
$template = $s;
|
||||
$s = new FriendicaSmarty();
|
||||
if (!Strings::startsWith($template, self::FILE_PREFIX)) {
|
||||
$template = self::STRING_PREFIX . $template;
|
||||
}
|
||||
|
||||
$r['$APP'] = DI::app();
|
||||
|
||||
// "middleware": inject variables into templates
|
||||
$arr = [
|
||||
"template" => basename($s->filename),
|
||||
"vars" => $r
|
||||
'template' => basename($this->smarty->filename),
|
||||
'vars' => $vars
|
||||
];
|
||||
Hook::callAll("template_vars", $arr);
|
||||
$r = $arr['vars'];
|
||||
Hook::callAll('template_vars', $arr);
|
||||
$vars = $arr['vars'];
|
||||
|
||||
foreach ($r as $key => $value) {
|
||||
foreach ($vars as $key => $value) {
|
||||
if ($key[0] === '$') {
|
||||
$key = substr($key, 1);
|
||||
}
|
||||
|
||||
$s->assign($key, $value);
|
||||
$this->smarty->assign($key, $value);
|
||||
}
|
||||
return $s->parsed($template);
|
||||
|
||||
return $this->smarty->fetch($template);
|
||||
}
|
||||
|
||||
public function getTemplateFile($file, $subDir = '')
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function getTemplateFile(string $file, string $subDir = '')
|
||||
{
|
||||
$a = DI::app();
|
||||
$template = new FriendicaSmarty();
|
||||
|
||||
// Make sure $root ends with a slash /
|
||||
if ($subDir !== '' && substr($subDir, -1, 1) !== '/') {
|
||||
$subDir = $subDir . '/';
|
||||
|
@ -80,21 +93,20 @@ class FriendicaSmartyEngine implements ITemplateEngine
|
|||
|
||||
$root = DI::basePath() . '/' . $subDir;
|
||||
|
||||
$theme = $a->getCurrentTheme();
|
||||
$filename = $template::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
|
||||
$filename = $this->smarty::SMARTY3_TEMPLATE_FOLDER . '/' . $file;
|
||||
|
||||
if (file_exists("{$root}view/theme/$theme/$filename")) {
|
||||
$template_file = "{$root}view/theme/$theme/$filename";
|
||||
} elseif (!empty($a->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename))) {
|
||||
$template_file = sprintf('%sview/theme/%s}/%s', $root, $a->theme_info['extends'], $filename);
|
||||
if (file_exists("{$root}view/theme/$this->theme/$filename")) {
|
||||
$template_file = "{$root}view/theme/$this->theme/$filename";
|
||||
} elseif (!empty($this->theme_info['extends']) && file_exists(sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename))) {
|
||||
$template_file = sprintf('%sview/theme/%s}/%s', $root, $this->theme_info['extends'], $filename);
|
||||
} elseif (file_exists("{$root}/$filename")) {
|
||||
$template_file = "{$root}/$filename";
|
||||
} else {
|
||||
$template_file = "{$root}view/$filename";
|
||||
}
|
||||
|
||||
$template->filename = $template_file;
|
||||
$this->smarty->filename = $template_file;
|
||||
|
||||
return $template;
|
||||
return self::FILE_PREFIX . $template_file;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue