Tpl: add fetch() method

This commit is contained in:
Benjamin Renard 2023-02-27 18:58:31 +01:00
parent 3742ce7448
commit 24e3101dd0
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC

View file

@ -444,6 +444,44 @@ class Tpl {
$sentry_span->finish();
}
/**
* Fetch a template
* @param string $template The template to fetch
* @param string|null $pagetitle The page title (optional)
* @param array $extra_args Extra arguments to use to compute the page title using sprintf
* @return string|false
*/
public static function fetch($template, $pagetitle=null, ...$extra_args) {
if (!$template) {
Log :: warning("Tpl::fetch(): No template specified.");
return false;
}
$sentry_span = new SentrySpan('smarty.fetch_template', "Fetch Smarty template");
// If extra arguments passed, format pagetitle using sprintf
if ($pagetitle && $extra_args) {
$pagetitle = call_user_func_array(
'sprintf',
array_merge(array($pagetitle), $extra_args)
);
}
$result = false;
$success = false;
try {
Hook :: trigger('before_fetching_template');
self :: define_common_variables($pagetitle);
$result = self :: $smarty->fetch("Tpl:$template");
$success = true;
}
catch (Exception $e) {
Log :: exception($e, "Smarty - An exception occured fetching template '$template'");
}
Hook :: trigger('after_fetching_template', array('success' => $success));
$sentry_span->finish();
return $success?$result:false;
}
/**
* Display AJAX return
* @param array|null $data AJAX returned data (optional)