From 24e3101dd063ce299d930cfcf8e140862d592234 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 27 Feb 2023 18:58:31 +0100 Subject: [PATCH] Tpl: add fetch() method --- src/Tpl.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/Tpl.php b/src/Tpl.php index ac8ec8a..6e16dbb 100644 --- a/src/Tpl.php +++ b/src/Tpl.php @@ -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)