From: Tim Otten Date: Thu, 10 Jul 2014 03:23:52 +0000 (-0700) Subject: CRM-14949 - CRM_Core_Page_AJAX::returnDynamicJS() => CRM_Core_Smarty::fetchWith() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=9b7526a8ba69601ab436e0b357c190f7ee1f18a3;p=civicrm-core.git CRM-14949 - CRM_Core_Page_AJAX::returnDynamicJS() => CRM_Core_Smarty::fetchWith() The returnDynamicJS() looks like sugar for Smarty's assign()+fetch() -- don't see anything about it that's particular to dynamic-JS. --- diff --git a/CRM/Admin/Page/AJAX.php b/CRM/Admin/Page/AJAX.php index 68b71b1a31..ebc458f0b3 100644 --- a/CRM/Admin/Page/AJAX.php +++ b/CRM/Admin/Page/AJAX.php @@ -51,7 +51,7 @@ class CRM_Admin_Page_AJAX { header('Content-Type: application/javascript'); header("Cache-Control: max-age=$year, public"); - print CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/navigation.js.tpl', array( + print CRM_Core_Smarty::singleton()->fetchWith('CRM/common/navigation.js.tpl', array( 'navigation' => CRM_Core_BAO_Navigation::createNavigation($contactID), )); } diff --git a/CRM/Core/Page/AJAX.php b/CRM/Core/Page/AJAX.php index b9ea566d03..593beb5f3c 100644 --- a/CRM/Core/Page/AJAX.php +++ b/CRM/Core/Page/AJAX.php @@ -199,19 +199,6 @@ class CRM_Core_Page_AJAX { CRM_Utils_System::civiExit(); } - /** - * Render and output a template as a javascript file - * @param string $tplFile - * @param array $vars - template variables - */ - static function returnDynamicJS($tplFile, $vars = array()) { - $smarty = CRM_Core_Smarty::singleton(); - foreach ($vars as $name => $val) { - $smarty->assign($name, $val); - } - return $smarty->fetch($tplFile); - } - /** * Send autocomplete results to the client. Input can be a simple or nested array. * @param array $results - If nested array, also provide: diff --git a/CRM/Core/Resources.php b/CRM/Core/Resources.php index cb6381684a..40b6bd20c2 100644 --- a/CRM/Core/Resources.php +++ b/CRM/Core/Resources.php @@ -570,7 +570,7 @@ class CRM_Core_Resources { 'otherSearch' => json_encode(ts('Enter search term...')), 'contactCreate' => CRM_Core_BAO_UFGroup::getCreateLinks(), ); - CRM_Utils_File::addDynamicResource($fileName, CRM_Core_Page_AJAX::returnDynamicJS('CRM/common/localization.js.tpl', $vars)); + CRM_Utils_File::addDynamicResource($fileName, CRM_Core_Smarty::singleton()->fetchWith('CRM/common/localization.js.tpl', $vars)); } /** diff --git a/CRM/Core/Smarty.php b/CRM/Core/Smarty.php index 9396326f3c..f683c56574 100644 --- a/CRM/Core/Smarty.php +++ b/CRM/Core/Smarty.php @@ -209,6 +209,27 @@ class CRM_Core_Smarty extends Smarty { return $output; } + /** + * Fetch a template (while using certain variables) + * + * @param string $resource_name + * @param array $vars (string $name => mixed $value) variables to export to Smarty + * @throws Exception + * @return bool|mixed|string + */ + function fetchWith($resource_name, $vars) { + $this->pushScope($vars); + try { + $result = $this->fetch($resource_name); + } catch (Exception $e) { + // simulate try { ... } finally { ... } + $this->popScope(); + throw $e; + } + $this->popScope(); + return $result; + } + /** * @param $name * @param $value